Connecting Tech Pros Worldwide Help | Site Map

var e = form.elements;

  #1  
Old July 23rd, 2005, 06:36 PM
WindAndWaves
Guest
 
Posts: n/a
Hi Gurus

I have the following statement in my code:

var e = form.elements;

Is it possible to put any conditions on this statement. What I actually want to say is:

var e = form.elements BUT ONLY FOR CHECKBOXES THAT START WITH R.

IN that way <INPUT TYPE="checkbox" NAME="r23"> would be part of e, but <INPUT TYPE="submit">, etc... would not be included



TIA



- Nicolaas


  #2  
Old July 23rd, 2005, 06:36 PM
RobB
Guest
 
Posts: n/a

re: var e = form.elements;


WindAndWaves wrote:[color=blue]
> Hi Gurus
>
> I have the following statement in my code:
>
> var e = form.elements;
>
> Is it possible to put any conditions on this statement. What I[/color]
actually want to say is:[color=blue]
>
> var e = form.elements BUT ONLY FOR CHECKBOXES THAT START WITH R.
>
> IN that way <INPUT TYPE="checkbox" NAME="r23"> would be part of e,[/color]
but <INPUT TYPE="submit">, etc... would not be included[color=blue]
>
>
>
> TIA
>
>
>
> - Nicolaas[/color]


var e = form.elements

....simply creates a reference to the elements (array) object of,
presumably, a specific form. Object references, naturally, have no
conditions - they only indicate a memory address where data is stored.
What you need to do is 1) create a custom collection (a subset of
Form.elements[]) filtered for only the elements you desire, or 2)
filter at run-time to exclude unwanted controls.

1)

var els = document.forms[0].elements,
rBoxes = [], //collection
el,
i = 0;
while (el = els[i++])
if (el.type == 'checkbox'
&& /^r/i.test(el.name))
rBoxes.push(el);
2)

if (el.type == 'checkbox'
&&/^r/i.test(el.name))
{...do something with it

  #3  
Old July 23rd, 2005, 06:36 PM
WindAndWaves
Guest
 
Posts: n/a

re: var e = form.elements;



"RobB" <ferndoc9@hotmail.com> wrote in message news:1107734964.841295.99380@c13g2000cwb.googlegro ups.com...[color=blue]
> WindAndWaves wrote:[color=green]
> > Hi Gurus
> >
> > I have the following statement in my code:
> >
> > var e = form.elements;
> >
> > Is it possible to put any conditions on this statement. What I[/color]
> actually want to say is:[color=green]
> >
> > var e = form.elements BUT ONLY FOR CHECKBOXES THAT START WITH R.
> >
> > IN that way <INPUT TYPE="checkbox" NAME="r23"> would be part of e,[/color]
> but <INPUT TYPE="submit">, etc... would not be included[color=green]
> >
> >
> >
> > TIA
> >
> >
> >
> > - Nicolaas[/color]
>
>
> var e = form.elements
>
> ...simply creates a reference to the elements (array) object of,
> presumably, a specific form. Object references, naturally, have no
> conditions - they only indicate a memory address where data is stored.
> What you need to do is 1) create a custom collection (a subset of
> Form.elements[]) filtered for only the elements you desire, or 2)
> filter at run-time to exclude unwanted controls.
>
> 1)
>
> var els = document.forms[0].elements,
> rBoxes = [], //collection
> el,
> i = 0;
> while (el = els[i++])
> if (el.type == 'checkbox'
> && /^r/i.test(el.name))
> rBoxes.push(el);
> 2)
>
> if (el.type == 'checkbox'
> &&/^r/i.test(el.name))
> {...do something with it
>[/color]
Cool thanks, I will push them I think. Great. Thanks for your help.


  #4  
Old July 23rd, 2005, 06:37 PM
Grant Wagner
Guest
 
Posts: n/a

re: var e = form.elements;


"WindAndWaves" <access@ngaru.com> wrote in message
news:f%FNd.15724$mo2.1234540@news.xtra.co.nz...[color=blue][color=green]
>> var els = document.forms[0].elements,
>> rBoxes = [], //collection
>> el,
>> i = 0;
>> while (el = els[i++])
>> if (el.type == 'checkbox'
>> && /^r/i.test(el.name))
>> rBoxes.push(el);
>> 2)
>>
>> if (el.type == 'checkbox'
>> &&/^r/i.test(el.name))
>> {...do something with it
>>[/color]
> Cool thanks, I will push them I think. Great. Thanks for your help.[/color]

If you know what you want to do with the form elements right away, then
why pass through the elements collection once, then pass through the
list of matching elements a second time?

var f;
if ((f = document.forms) &&
(f = f['yourForm']) &&
(f = f.elements))
{
var ii = f.length;
while (ii-- > 0)
// or
// for (var ii = 0; i < f.length; ++ii)
// if direction matters
{
if (f[ii].type == 'checkbox' &&
/^r/i.test(f[ii].name))
{
// do something with f[ii]
}
}
}

--
Grant Wagner <gwagner@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq


  #5  
Old July 23rd, 2005, 06:38 PM
WindAndWaves
Guest
 
Posts: n/a

re: var e = form.elements;



"Grant Wagner" <gwagner@agricoreunited.com> wrote in message news:qaMNd.179$3d3.550@news2.mts.net...[color=blue]
> "WindAndWaves" <access@ngaru.com> wrote in message
> news:f%FNd.15724$mo2.1234540@news.xtra.co.nz...[color=green][color=darkred]
> >> var els = document.forms[0].elements,
> >> rBoxes = [], //collection
> >> el,
> >> i = 0;
> >> while (el = els[i++])
> >> if (el.type == 'checkbox'
> >> && /^r/i.test(el.name))
> >> rBoxes.push(el);
> >> 2)
> >>
> >> if (el.type == 'checkbox'
> >> &&/^r/i.test(el.name))
> >> {...do something with it
> >>[/color]
> > Cool thanks, I will push them I think. Great. Thanks for your help.[/color]
>
> If you know what you want to do with the form elements right away, then
> why pass through the elements collection once, then pass through the
> list of matching elements a second time?
>
> var f;
> if ((f = document.forms) &&
> (f = f['yourForm']) &&
> (f = f.elements))
> {
> var ii = f.length;
> while (ii-- > 0)
> // or
> // for (var ii = 0; i < f.length; ++ii)
> // if direction matters
> {
> if (f[ii].type == 'checkbox' &&
> /^r/i.test(f[ii].name))
> {
> // do something with f[ii]
> }
> }
> }
>
> --
> Grant Wagner <gwagner@agricoreunited.com>
> comp.lang.javascript FAQ - http://jibbering.com/faq
>
>[/color]

Thank you Grant.


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
JS error: form.elements[...].checked is null or not an object mars123 answers 2 November 27th, 2007 11:14 AM
newb: recurse over elements children and disable all form elements SteveKlett@gmail.com answers 4 September 20th, 2006 08:35 AM
keep form elements enabled on "back" request Dani answers 5 July 23rd, 2005 10:01 PM