SoFunction
Updated on 2025-02-28

js implements the method of selecting all, not selecting and unselecting checkbox

This article describes the method of JS to implement checkbox selection, non-select and anti-select. Share it for your reference. The specific analysis is as follows:

1. Ideas:

1. Get elements

2. Select all, don't select, add click event

3. Loop checkbox with for

4. Set checkbox checked to true to select all

5. Set checkbox checked to false to achieve no selection

6. Judging by if, if checked is true, set checked to false and unselected state, and if checked is false and unselected state, set checked to true and unselected state.

2. HTML code:

<input type="button" value="Select All" />
<input type="button" value="Not Select" />
<input type="button" value="Anti-select" />
<div >
    <input type="checkbox"/><br />
    <input type="checkbox"/><br />
    <input type="checkbox"/><br />
    <input type="checkbox"/><br />
    <input type="checkbox"/><br />
    <input type="checkbox"/><br />
    <input type="checkbox"/><br />
    <input type="checkbox"/><br />
    <input type="checkbox"/><br />
    <input type="checkbox"/><br />
    <input type="checkbox"/><br />
    <input type="checkbox"/><br />
    <input type="checkbox"/><br />
    <input type="checkbox"/><br />
    <input type="checkbox"/><br />
    <input type="checkbox"/><br />
</div>

3. js code:

<script>
=function(){

  var sele=('sele');//Get all selected  var unsele=('setinterval');//No selection for obtaining  var clear=('clear');//Get the reverse selection  var checkbox=('checkboxs');//Get div  var checked=('input');//Get the input under the div//Select all   =function(){
    for(i=0;i<;i++){
    checked[i].checked=true
          }
     }

//Not selected    =function(){
      for(i=0;i<;i++){
       checked[i].checked=false
          }
      }
//Reverse selection    =function(){
       for(i=0;i<;i++){
        if(checked[i].checked==true){
        checked[i].checked=false
         } 
   else{
     checked[i].checked=true
       }
       }
      }
}
</script>

I hope this article will be helpful to everyone's JavaScript programming.