How to disable/ enable checkbox with jQuery?


fTo disable and enable checkbox, use the attr() method.

Initially set the input type checkbox and disable it −

<input type="checkbox" id="sName" name= "Male" style="width: 25px" value="male" disabled="disabled"/>Male

Now on the click of a button, toggle between disabled and enabled checkbox −

$("#button1").click(function() {
   $("#sName").attr('disabled', !$("#sName").attr('disabled'));
});

Example

 Live Demo

<html>
<head>
<title>jQuery Example</title>
   <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
   <script>
      $(document).ready(function() {
         $("#button1").click(function() {
            $("#sName").attr('disabled', !$("#sName").attr('disabled'));
         });
      });
   </script>
</head>

<body>
<input type="checkbox" id="sName" name= "Male" style="width: 25px" value="male" disabled="disabled"/>Male
<br>
<br>
<input type="button" id="button1" value="Disable/ Enable" />
</body>
</html>

Updated on: 20-Jun-2020

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements