If you are using jQuery and want to find out if a particular checkbox is checked, inside jQuery there are some fairly straightforward ways to do this. In this article, we will explore some of them, along with some examples.
jQuery – checking a checkbox with is ( ‘:checked’)
The first way to check this is by using the function is () in jQuery. It checks if the argument or set of arguments that you stated satisfies the given condition. If so, it returns “true”. If not, it returns “false”.
To use is (), we first need to choose the element and then do the check using the selector :checked, that works with several elements, including checkboxes. Example below:
jQuery – checking a checkbox with prop ( ‘:checked’)
Before jQuery 1.6 the function attr () was used to obtain both attributes as the properties of an element. This creates some confusion, so much so that after jQuery 1.6 a new function, called prop () was designed to verify the property of the element analyzed.
A short explanation about the difference between attributes and properties. Simply put, the attribute can be considered the value declared in the code, as the property is the value when the check is done. If the user changes this value, such happens with a checkbox, for example, what you want is the property of this element. Example using prop ():
Do you know other ways to check if a checkbox is checked using jQuery? Share with us in the comments area at the bottom!
If you want to explore more about jQuery, visit our section of videos! Below are some examples:
You can also follow some of the broadcasters who program in jQuery, as below:
Another cool way to find out interesting things about jQuery is to access our jQuery project page!