In Action

Javascript Example to Check / Uncheck Checkboxes

Simple javascript example demonstrating how to check or uncheck all checkboxes.

Javascript in Action


Make a selection
Java
JavaScript
ASP
HTML
SQL



Javascript Code

Javascript Code

Just cut-n-paste to reuse.

 
     
<script type="text/javascript">
var checkflag = "false";
function check(field) {
  if (checkflag == "false") {
    for (i = 0; i < field.length; i++) {
      field[i].checked = true;
    }
    checkflag = "true";
    return "Uncheck All";
  } else {
    for (i = 0; i < field.length; i++) {
      field[i].checked = false;
    }
    checkflag = "false";
    return "Check All";
  }
}
</script>

                                                                                                                                       

HTML Code

HTML Code



<form name=myform action="" method=post>
<table>
  <tr><td>
    <strong>Make a selection</strong><br>
    <input type=checkbox name=list value="1">Java<br>
    <input type=checkbox name=list value="2">JavaScript<br>
    <input type=checkbox name=list value="3">ASP<br>
    <input type=checkbox name=list value="4">HTML<br>
    <input type=checkbox name=list value="5">SQL<br>
    <br>                                                    
    <input type=button value="Check All" onClick="this.value=check(this.form.list)"> 
  </td></tr>
</table>
</form>