PDA

View Full Version : Loop through Checkboxes and see if checked problem


RealBeginner
10-07-2002, 11:26 AM
Hi All,

I am trying to write a function that will indicate to me whether or not the checkboxes on my web page are checked or not.
I am using a database and the number of checkboxes can vary depending on what dates the user selected. I have a button that has an onClick event to run the tester function. The checkboxes are called "chbPlanned<%number%>".

I want to go through the form elements that's name starts with "chbPlanned" and that are checked and then have a pop-up with the element name and whether or not it is checked.

Below is the function I have written but I keep getting problems. Can someone help me please.


function tester()
{
temp = document.formName.elements.length ;
for (i=0; i<temp; i++) {
if ((leftcontext(document.formName.elements[i].name,10) == "chbPlanned")) && ((document.formName.elements[i].checked == 1) {window.alert(document.formName.elements[i].name +" is"))}

else {window.alert(document.formName.elements[i].name +" not")}
}

}

Thanks in advance.

glenngv
10-07-2002, 12:00 PM
first of all, the checkboxes should be named "chbPlanned<%=number%>"



function tester()
{
for (i=0; i<document.formName.elements.length; i++) {
el = document.formName.elements[i];
if (el.type=="checkbox" && el.name.indexOf("chbPlanned")!=-1){
if (el.checked) alert(el.name +" is checked.");
else alert(el.name +" is not checked.");
}
}

}