Toggle checkboxes

Submitted by:Jhon Smith

Date added:22 September, 2016

Category:HTML

A short javascript function to toggle checkboxes

Tags: checkboxes , toggle

Code Snippet:

<!------>

<script type="text/javascript">

function toggle_checkboxes(id) {
if (!document.getElementById){ return; }
if (!document.getElementsByTagName){ return; }
var inputs = document.getElementById(id).getElementsByTagName("input");
for(var x=0; x < inputs.length; x++) {
if (inputs[x].type == 'checkbox'){
inputs[x].checked = !inputs[x].checked;
}
}
}

</script>

<div id="parent_box">

<input type="checkbox" name="foo" value="1" /> 1<br/>
<input type="checkbox" name="foo" value="2" checked="checked" /> 2<br/>
<input type="checkbox" name="foo" value="3" checked="checked" /> 3<br/>

<br/>
<input type="button" value="Toggle checkboxes"
onclick="toggle_checkboxes('parent_box')" />

</div>
 
 

Comments