Tuesday, July 25, 2017

How to check/uncheck checkbox using jQuery? Example Tutorial

In the last couple of articles, I have shared a couple of useful jQuery tips e.g. reloading web page and working with tag selectors, today, I'll show you how to check or uncheck a particular checkbox using jQuery. If you remember, in HTML a checkbox is checked if the "checked" attribute is present and its value is not false, otherwise, it's unchecked. By using jQuery function prop() we can dynamically add this attribute or if present we can change its value i.e. checked=true to make the checkbox checked and checked=false to mark the check box unchecked. Though you should remember that prop() function is only available from jQuery 1.6 version and if you are running on lower jQuery version then you can also use attr() to check/uncheck a particular checkbox.

check/uncheck checkbox using jQuery

Here is a simple jQuery example to show you how to check and uncheck a particular checkbox. In this example, we are selecting one particular check box using ID selector, if you want to select multiple check boxes.

You can also use a class selector if there is a class applied to all the check boxes or you can also use element selector like input[type='checkbox'] to select all check boxes. Depending upon whether you want to apply the change on one element or multiple elements, there are a couple of ways to do it in jQuery. I suggest you read Head First jQuery to see the full list of selectors.

Once the checkbox is selected, we are calling prop() function as prop( "checked", true ) to check the checkbox and prop( "checked", false ) to uncheck the checkbox.

Here is a working example:

      <title>How to check/uncheck a checkbox using jQuery</title>
      <h2>Checking and Unchecking a Check box using jQuery</h2>
      <div id="plateform">
         <input id = "myCheckbox" type="checkbox"> A CheckBox</input>
      <button id="btn_check">check the checkbox</button>
      <button id="btn_uncheck">uncheck the checkbox</button>
      <script src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
         $(document).ready(function() {
         $("#myCheckbox").prop("checked", true);
         $("#myCheckbox").prop("checked", false);

When you save this code as test.html and open in a browser e.g. internet explorer, chrome, firefox, or windows edge, you will see the following screen:

How to check/uncheck checkbox using jQuery

You can see there are two buttons and a checkbox is present in the page. If you click the "check the checkbox" button then the checkbox will be checked, as shown in the following screenshot:

How to select a checkbox in jQuery

Similarly, if you click the "uncheck the checkbox" button then the checkbox will be unchecked as shown in the following screenshot:

How to uncheck a checkbox using jQuery

That's all about how to check/uncheck a checkbox using jQuery. You can also apply the same logic to check/uncheck a radio button as well. If you have to check multiple checkboxes, instead of ID selector you can either use a class selector, of-course only if you have a CSS class applied to them or you can use element selector e.g. input[type="checkbox"] to select multiple checkboxes and then call prop() on them.

 Other jQuery articles for you
  • 5 Books to learn jQuery for Web developers (books)
  • How to get current URL, Parameters, and Hash using jQuery? (solution)
  • How to modify more than one element in one line? (example)
  • jQuery Hello World Example (program)
  • Free jQuery eBooks and PDFs (download)
  • How to use more than one jQuery UI DatePicker in JSP page? (example)
  • How to create tab-based UI using jQuery? (example)
  • Top 20 jQuery Interview Questions for Java Web Developers (list)
  • How to redirect a page URL using jQuery and JavaScript? (solution)
  • How to download jQuery dynamically in a web page? (solution)

Further Reading
The Complete jQuery Course: From Beginner To Advanced!
Up and Running with jQuery
jQuery Fundamentals By Dan Wahlin

Thanks for reading this article so far. If you like this jQuery tutorial then please share with your friends and colleagues. If you have any question or feedback then please drop a comment.

No comments :

Post a Comment