Wednesday, 11 February 2015

Snippet: Check All


Hello people this is a simple snippet written in javascript. The code is check all and clear all checkbox.
Below is the code:

<html>
<head>
<script type="text/javascript">
function checkAll(checkEm) {
    var cbs = document.getElementsByTagName('input');
    for (var i = 0; i < cbs.length; i++) {
        if (cbs[i].type == 'checkbox') {
            if (cbs[i].name == 'menu[]') {
                cbs[i].checked = checkEm;
            }
        }
    }
}
</script>
</head>
<body>
<form action="" method="post">
<input type="checkbox" name="menu[]" value="menu1" style="width: 16px;">Menu 1<br>
<input type="checkbox" name="menu[]" value="menu2" style="width: 16px;">Menu 2<br>
<input type="checkbox" name="menu[]" value="menu3" style="width: 16px;">Menu 3<br>
<input type="checkbox" name="menu[]" value="menu4" style="width: 16px;">Menu 4<br>
<input type="button" class="my_form-check-button" onClick="checkAll(true);" value="Check All"/>
<input type="button" class="my_form-check-button" onClick="checkAll(false);" value="Clear All"/>
</form>
</body>
</html>

Hope you learn from this simple code.

No comments:

Post a Comment