Lets say I have a checkbox with id ‘test’ and its on a page loaded with jQuery 1.5.2.
<input id="test" type="checkbox"/>
Now if I click on the checkbox to set it in the checked state…
Why does $("#test").is(":checked") return true and $("#test").is("*[checked]") return false.
Here’s a jsFiddle with the described scenario
The
[checked]is a selector that picks elements that are currently checked with thecheckedattribute not the property – in other words “if the element has the checked attribute, pick it up”.If you want to check if the checkbox is checked or not, you must use
:checked.Example usage:
With jQuery 1.6 and higher you can use the
.prop()function for performance reasons.