Connecting Tech Pros Worldwide Help | Site Map

var e = form.elements;

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 05:36 PM
WindAndWaves
Guest
 
Posts: n/a
Default var e = form.elements;

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, 05:36 PM
RobB
Guest
 
Posts: n/a
Default 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, 05:36 PM
WindAndWaves
Guest
 
Posts: n/a
Default 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, 05:37 PM
Grant Wagner
Guest
 
Posts: n/a
Default 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, 05:38 PM
WindAndWaves
Guest
 
Posts: n/a
Default 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.


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.