How to check or uncheck a checkbox dynamically using jQuery

Topic: JavaScript / jQuery« Prev|Next »

Answer: Use the jQuery prop() method

You can use the jQuery prop() method to check or uncheck a checkbox dynamically such as on click of button or an hyperlink etc. The prop() method require jQuery 1.6 and above.

  • <!DOCTYPE html>
  • <html lang="en">
  • <head>
  • <meta charset="UTF-8">
  • <title>jQuery Check/Uncheck Checkbox</title>
  • <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
  • <script type="text/javascript">
  • $(document).ready(function(){
  •     $(".check").click(function(){
  •         $("#myCheck").prop("checked", true);
  •     });
  •     $(".uncheck").click(function(){
  •         $("#myCheck").prop("checked", false);
  •     });
  • });
  • </script>
  • </head>
  • <body>
  •     <p><input type="checkbox" id="myCheck"> Are you sure?</p>
  •     <button type="button" class="check">Yes</button>
  •     <button type="button" class="uncheck">No</button>  
  • </body>
  • </html>

Related FAQ

Here are some more FAQ related to this topic:

 
Close

Your Feedback:

 

We would love to hear from you! Please say something.