Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
How to set “checked” for a checkbox with jQuery?
Use the checked attribute to set the checkbox with jQuery. The prop() method is the recommended approach for getting and setting properties like checked, while attr() handles HTML attributes.
Setting Checkbox to Checked
To programmatically check a checkbox, use the prop() method with the value true ?
// Check a checkbox
$("#myCheckbox").prop("checked", true);
// Uncheck a checkbox
$("#myCheckbox").prop("checked", false);
Complete Example
You can try to run the following code to learn how to set checked for a checkbox ?
jQuery Checkbox Example
Key Differences
Understanding the difference between these methods is important ?
-
prop("checked")? Returns the current state (true/false) -
attr("checked")? Returns the HTML attribute value -
is(":checked")? Returns boolean for current checked state
Conclusion
Use prop("checked", true) to check a checkbox and prop("checked", false) to uncheck it. The prop() method is preferred over attr() for boolean properties in modern jQuery.
Advertisements
