Javascript Disable Checkbox when another Checkbox is checked 

Uncheck other Checkboxes on selection of one Checkbox in Javascript

 

Hi All! In this article, we will be seeing how to Disable Checkbox when another Checkbox is checked in Javascript i.e., how to make checkboxes behave like radio buttons.
The simplest approach is by using vanilla Javascript. Get all the checkboxes in the form of an array and then looping over each item and checking each item in the array matches with the source checkbox which is clicked and if not, uncheck the checkboxes which are not clicked/selected.
 

 <!DOCTYPE html>  
 <html>  
     <head>  
        <script>  
             function check(el){  
                 var cities = document.getElementsByName("city");  
                 cities.forEach(function(item){  
                     if(item!=el)  
                         item.checked = false;  
                 });  
             }  
         </script>  
      </head>  
      <body>  
          <h1>Radio button like Checkboxes</h1>  
          <input type="checkbox" onchange=check(this) id="city1" name="city" value="Hyderabad">Hyderabad<br>  
          <input type="checkbox" onchange=check(this) id="city2" name="city" value="Mumbai">Mumbai<br>  
          <input type="checkbox" onchange=check(this) id="city3" name="city" value="Delhi">Delhi  
      </body>  
 </html>  

Output

Uncheck other Checkboxes on selection of one Checkbox in Javascript