By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,339 Members | 1,688 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,339 IT Pros & Developers. It's quick & easy.

var e = form.elements;

P: 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
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
WindAndWaves wrote:
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

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

Jul 23 '05 #2

P: n/a

"RobB" <fe******@hotmail.com> wrote in message news:11*********************@c13g2000cwb.googlegro ups.com...
WindAndWaves wrote:
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

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

Cool thanks, I will push them I think. Great. Thanks for your help.
Jul 23 '05 #3

P: n/a
"WindAndWaves" <ac****@ngaru.com> wrote in message
news:f%*********************@news.xtra.co.nz...
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

Cool thanks, I will push them I think. Great. Thanks for your help.


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 <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #4

P: n/a

"Grant Wagner" <gw*****@agricoreunited.com> wrote in message news:qa***************@news2.mts.net...
"WindAndWaves" <ac****@ngaru.com> wrote in message
news:f%*********************@news.xtra.co.nz...
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

Cool thanks, I will push them I think. Great. Thanks for your help.


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 <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq


Thank you Grant.
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.