PDA

View Full Version : problems changing names in loop


charbort
01-29-2003, 03:31 PM
I have the following code and am having trouble revising it to make it work with changed checkbox input names. I want to change the names from all being "checkbox" to "subject1", "subject2",..... etc.

Any suggestions?

Thanks,
charbort



<script Language="JavaScript">
<!--
function checkbox_checker()
{

// set var checkbox_choices to zero

var checkbox_choices = 0;

// Loop from zero to the one minus the number of checkbox button selections
for (counter = 0; counter < checkbox_form.checkbox.length; counter++)
{

// If a checkbox has been selected it will return true
// (If not it will return false)
if (checkbox_form.checkbox[counter].checked)
{ checkbox_choices = checkbox_choices + 1; }

}


if (checkbox_choices < 1 )
{
// If there were no selections made display an alert box
msg="Please make a selection.\n"
msg=msg + "You have made " + checkbox_choices + " selections.\n"
msg=msg + "Please make at least one selection."
alert(msg)
return (false);
}



// If three were selected then display an alert box stating input was OK
alert(" *** Valid input was entered. ***");
return (true);
}

-->
</script>


<form method="get" action="#"
onsubmit="return checkbox_checker()" name="checkbox_form">
<input type="checkbox" value="Submit1" name="checkbox">Subscriber 1<br>
<input type="checkbox" value="Submit2" name="checkbox">Subscriber 2<br>
<input type="checkbox" value="Submit3" name="checkbox">Subscriber 3<br>
<input type="checkbox" value="Submit4" name="checkbox">Subscriber 4<br>
<input type="checkbox" value="Submit5" name="checkbox">Subscriber 5<br>
<input type="submit" value="Submit">
</form>

head8k
01-29-2003, 03:52 PM
How about doing it like this. This will make sure at least one checkbox is ticked:

<script>
<!--
function checkbox_checker(f){

counter=0;

for(i=0;i<f.elements.length;i++){
if((f.elements[i].type=='checkbox')&&(f.elements[i].checked))
counter++;
}

if(counter==0){
alert('check some boxes');
return false;
}
return true;
}
//-->
</script>


<form method="get" action="whatever" onsubmit="return checkbox_checker(this)" name="checkbox_form">
<input type="checkbox" value="Submit1" name="Submit 1">Subscriber 1<br>
<input type="checkbox" value="Submit2" name="Submit 2">Subscriber 2<br>
<input type="checkbox" value="Submit3" name="Submit 3">Subscriber 3<br>
<input type="checkbox" value="Submit4" name="Submit 4">Subscriber 4<br>
<input type="checkbox" value="Submit5" name="Submit 5">Subscriber 5<br>
<input type="submit" value="Submit">
</form>

charbort
01-29-2003, 06:32 PM
thanks head8k