Asked 10 years ago. Active 2 months ago. Viewed 1. Improve this question. Todua Clinton Green Clinton Green 9, 18 18 gold badges 61 61 silver badges bronze badges. The jQuery object has no property checked.
Add a comment. Active Oldest Votes. Improve this answer. SLaks SLaks k gold badges silver badges bronze badges. Code confirmed by jQuery doc, "the cross-browser-compatible way to determine if a checkbox is checked is to use the property: if elem. If we are going to be talking about performance then some of these comments should be added directly to the question asked by Clinton, and suggesting that the absolute fastest recommendation would be to add an ID — Tom Stickel.
This example will print Not checked at the console since the checkbox is not checked or not. Hope this post was helpful. Click the clap below to appreciate. Skip to content Many times we need to show or hide an element based on the current state of a checkbox and for that we should first check whether the checkbox is checked or not.
Liked the article? Spread the word Leave a Reply Cancel reply Comment. The example we saw above only contained one checkbox; in real-world situations you'll be likely to encounter multiple checkboxes.
If they are completely unrelated, then you can just deal with them all separately, as shown above. However, if they're all related, things are not quite so simple.
For example, in the following demo we include multiple checkboxes to allow the user to select their interests see the full version in the Examples section.
In this example you will see that we've given each checkbox the same name. When this string reaches the server, you need to parse it other than as an associative array, so all values, not only the last value, of interest are captured. To make a checkbox checked by default, you give it the checked attribute. See the below example:. This is a really useful feature of HTML form labels that makes it easier to click the option you want, especially on small-screen devices like smartphones.
In addition to the checked and unchecked states, there is a third state a checkbox can be in: indeterminate. This is a state in which it's impossible to say whether the item is toggled on or off. There are not many use cases for this property. The most common is when a checkbox is available that "owns" a number of sub-options which are also checkboxes. If all of the sub-options are checked, the owning checkbox is also checked, and if they're all unchecked, the owning checkbox is unchecked.
If any one or more of the sub-options have a different state than the others, the owning checkbox is in the indeterminate state. This can be seen in the below example thanks to CSS Tricks for the inspiration.
0コメント