Click to See Complete Forum and Search --> : multiple checkbox question


bjnst6
November 14th, 2002, 10:56 PM
hey there...

i'm using IE 6...and i've got this javascript code:

var totalCheck = theForm.chkDelete.length;

alert(theForm.chkDelete.value);
alert(theForm.chkDelete.length);

okay...now i have this html on the page:

<input type="checkbox" name="chkDelete" value="#EventID#">

instead a cfoutput loop...so in a loop.

now when i have 2 or more checkboxes on the page it works fine...i get

chkDelete.value = (whatever the first EventID is)
chkDelete.length = 2

but when i have only one checkbox the .value works the same...but the length is undefined.

what's going on here and how can i test for just one box if it won't work this way?

thanks!
b

websmith99
November 18th, 2002, 01:27 PM
I'm assuming all of your checkboxes have the same name??

If so, they are referred to as indices in an array - chkDelete[0], chkDelete[1], etc.

The length property defines how many members are in the array.
When there is only one checkbox, the length is undefined because there have to be at least 2 members in any array in JavaScript.

For the special case of one checkbox, simply do:

if (theForm.chkDelete.length) {
// code for 2 or more checkboxes
for (i=0; i< theForm.chkDelete.length; i++) {
foo = theForm.chkDelete[i].value;
alert(foo);
}
} else {
// code for 1 checkbox
foo = theForm.chkDelete.value;
alert(foo);
}

bjnst6
November 18th, 2002, 02:28 PM
this is what i was thinking you'd say...i just couldn't find any documentation on it for sure. sounds like a good fix.

i should've know the websmith would know...again. thanks!