How to disable/ enable checkbox with jQuery?

jQueryWeb DevelopmentFront End Technology

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>
raja
Published on 09-Aug-2018 10:06:48
Advertisements