Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Syntax concatenation problem 2

Status
Not open for further replies.

jimmythegeek

Programmer
May 26, 2000
770
US
I am sure this is an easy syntax thing, but I can't seem to get it. This works fine:

function checkAll() {
for(var i=0; i<document.frm.chk1.length; i++) {
document.frm.chk1.checked=true;
}
}


However, I have sevaral check box control arrays (chk1, chk2, chk3, etc.), and want to be able to dynamically check them all, but the following bombs when I try to concatenate &quot;x&quot; onto the chk control (line 5), it says document.frm.chk is null or not an object.

function checkAll() {
var modes = 6
for(var x=1; x < modes; x++) {
for(var i=0; i<document.frm.chk1.length; i++) {
document.frm.chk[x].checked=true;
}
}
}

Thanks in advance.

Jim Lunde
compugeeks@hotmail.com
We all agree your theory is crazy, but is it crazy enough?
 
You might try accessing your arrays like this:

for (var x=1; x < modes; x++)
{
var cArrayName = &quot;chk&quot; + x;
var checkArray = document.elements[cArrayName];
for (var i=0; i < checkArray.length; i++)
{
checkArray.checked = true;
}

}

I've done some similar stuff dynamically accessing ids. I didn't test this, so there might be some syntax problems, but hopefully it will get you started in the right direction.

 
eval(&quot;document.frm.chk&quot; + x + &quot;.checked=true&quot;)

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook
 
Thank you for you responses.

Zebra,
I tried something similar, and could not get it to work. I got an error on your example (document.elements is null or is not an object).

mWolf
I just needed to add the inside the string and the eval worked nicely. Thank you.

Jim Lunde
compugeeks@hotmail.com
We all agree your theory is crazy, but is it crazy enough?
 
On reflection I think it should be document.forms[0].elements[cArrayNames].

But mWolf's solution is better. So I give him a star, too!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top