Toggle checkboxes

A short javascript function to toggle checkboxes

<!------>
 
<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>
Snippet Details




Sorry folks, comments have been deactivated for now due to the large amount of spam.

Please try to post your questions or problems on a related programming board, a suitable mailing list, a programming chat-room,
or use a QA website like stackoverflow because I'm usually too busy to answer any mails related
to my code snippets. Therefore please just mail me if you found a serious bug... Thank you!


Older comments:

None.