The easy way to check/uncheck checkboxes. Insert into <head></head> this script

<script type="text/javascript">
function check_all(){
   var all_inputs = document.getElementsByTagName("input");
 
   for(i=0; i<all_inputs.length; i++){
      if(all_inputs[i].type=="checkbox" && all_inputs[i].className=="check_uncheck"){
         all_inputs[i].checked = "checked";
      }
   }
}
 
function uncheck_all(){
   var all_inputs = document.getElementsByTagName("input");
 
   for(i=0; i<all_inputs.length; i++){
      if(all_inputs[i].type=="checkbox" && all_inputs[i].className=="check_uncheck"){
         all_inputs[i].checked = "";
      }
   }
}
</script>

then set “check_uncheck” the classes of checkboxes which must check/uncheck (Example: <input type=”checkbox” name=”checkbox”>) and set onclick=”check_all()” to element on click which the checkboxes will be checked and onclick=”uncheck_all()” to element on click which the checkboxes will be unchecked (Example: <input type=”button” value=”Check” onclick=”check_all()”> <input type=”button” value=”Uncheck” onclick=”uncheck_all()”>).

Share

1 Comment on Check/uncheck checkboxes by JavaScript

  1. Hayk says:

    Don’t work…

Leave a Reply

Info