Generally in the checkbox, we have two different type of checkboxes one are plain checkboxes and the second one are checkbox array. The javascript for both differs a lot. The default checkbox script will be very simple to write and can be found any where in the net. The checkbox array scripts would be difficult.
The below example would define how to write a javascript for a checkbox array.
Example: Use checkbox array and have the option to checkall and uncheckall.
<html>
<head>
<script language="javascript">
function anyCheck(form) {
var total = 0;
var max = document.form.elements['list[]'].length;
for (var idx = 0; idx<max; idx++) {
if(document.form.elements['list[]'][idx].checked==true)
{
if(document.form.elements['list[]'][idx].value==1)
{
for (var idx1 = 0; idx1<max; idx1++) {
document.form.elements['list[]'][idx1].checked=true;
}
}
}else{
if(document.form.elements['list[]'][idx].value==1)
{
for (var idx1 = 0; idx1<max; idx1++) {
document.form.elements['list[]'][idx1].checked=false;
}
}
}
//alert(total);
}
//alert('ns');
//alert('ns');
//alert("You selected " + total + " boxes.");
}
</script>
</head>
<body>
<form name="form" action="test.html" method="post">
<input type="checkbox" name="list[]" value="1" onClick="anyCheck(this.form)">Checkall<br>
<input type="checkbox" name="list[]" value="2">Javascript<br>
<input type="checkbox" name="list[]" value="3">Active Server Pages<br>
<input type="checkbox" name="list[]" value="4">HTML<br>
<input type="checkbox" name="list[]" value="5">SQL<br>
</form>
</body>
The script anyCheck() function will validate the checkboxes.
No comments:
Post a Comment