Monday, May 31, 2010

JavaScript Select all Checkbox

How to select all checkbox in form


Usage : Simple JavaScript for select and deselect all checkbox in form.
Compatibility :




JavaScript Code :
Insert this JavaScript in the head section of the html code.

<script type="text/javascript">
function select_all(option){
var no_of_elements = form1.elements.length;
for(loop=0; loop < no_of_elements; loop++){
form1.elements[loop].checked = option;
}
}
</script>

Html Code :

<html>
<head>
<title>Select All Check Box </title>
</head>
<body>
<form name="form1" id="form1" action="#" method="post">
checkbox 1<input type="checkbox" name="checkbox_item" id="checkbox_item" value="checkbox 1">
checkbox 2<input type="checkbox" name="checkbox_item" id="checkbox_item" value="checkbox 2">
checkbox 3<input type="checkbox" name="checkbox_item" id="checkbox_item" value="checkbox 3">
</form>
<input name="select_all" type="button" value="Select All" onclick="select_all('true')">
<input name="clear_all" type="button" value="Clear All" onclick="select_all('')">
</body>
</html>

No comments: