Wednesday, November 30, 2005

Javascript Issues


I am not sure why, but when I use the following function, if there is only one checkbox, it is not working. When I try to use alert and see the contents of attendeelength, it is returning undefined. Note: This happens only when I have one checkbox by name “attendees”, but if I have multiple checkboxes with same name, code works fine.

function CheckAll()
{
var count=0;
var attendeecheckbox = document.all.item("attendees");
var attendeelength = document.all.item("attendees").length;
while (count != attendeelength) {
attendeecheckbox(count).checked=true;
count++;
}
}



So, my solution was to use the following code instead of the above code. This code works with one or many checkboxes.

function CheckAll()
{
var count=0;
var attendeecheckbox = document.getElementsByName("attendees");
var attendeelength = attendeecheckbox.length;
while (count != attendeelength) {
attendeecheckbox(count).checked=true;
count++;
}
}

No comments: