Click to See Complete Forum and Search --> : numbers and spaces in field names


autumnnn
04-16-2004, 02:49 AM
hello,

I'm using this function to perform a "check all" action for checking all check boxes. The names of the checkboxes go like this:

test0
test1
test2
test3 ...

My script works really well but it breaks when the field name either starts with a number like:

5_test_it0

or if it has spaces like:

test it0

Can you please tell me how I can make it work? I can probably replace the spaces with underscores if I have to but I can't change the numbers.

function checkAll(t,f) {
// f is form name t is field name

for (var j = 0; j <= 100; j++) {
box = eval("document." +f+ "." +t + j);
mainbox = eval("document." +f+ "." +t);
if (mainbox.checked == false){
box.checked = false;
}else{
box.checked = true;
}
}
}


// check all checkbox
<input type="checkbox" name="5 page website" onclick="checkAll(this.name,'form1');" />


// the checkboxes
<input type="checkbox" name="5 page website0" value="yes" >

<input type="checkbox" name="5 page website1" value="yes" >

<input type="checkbox" name="5 page website2" value="yes" >


Thank you
A

Kor
04-16-2004, 03:42 AM
1. use the full referenece syntax
document.forms['name_or_order'].elements['name_or_order']
2. you don't need the eval
3. default checked value is true, no need to specify

try this:

function checkAll(t,f) {
// f is form name t is field name
for (var j = 0; j <= 100; j++) {
if (document.forms[f].elements[t].checked){
document.forms[f].elements[f+j].checked = true;
}else{
document.forms[f].elements[f+j].checked = false;
}
}
}

Kor
04-16-2004, 03:44 AM
sorry, mistiped f insted of t in elements names


function checkAll(t,f) {
// f is form name t is field name
for (var j = 0; j <= 100; j++) {
if (document.forms[f].elements[t].checked){
document.forms[f].elements[t+j].checked = true;
}else{
document.forms[f].elements[t+j].checked = false;
}
}
}

autumnnn
04-16-2004, 06:55 PM
worked like a charm!

Thank you very much :D

A