Check/uncheck checkboxes by JavaScript

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/Bookmark
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading ... Loading ...

Leave a Reply

*





Info