Bug Tracker

Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#7695 closed bug (invalid)

selector [checked] does not work when on applied to a single checkbox

Reported by: JordanClark Owned by:
Priority: undecided Milestone: 1.6
Component: unfiled Version: 1.4.4
Keywords: Cc:
Blocked by: Blocking:

Description

Versioning and affected browsers

In FF, Chrome and Safari, the behavior of the [checked] selector changed from 1.4.2 to 1.4.3, and the same behavior exists in 1.4.4
IE is not affected by the bug, and exhibits the same behavior from 1.4.2 - 1.4.4
The browser versions I used are FF 3.6.12, Chrome 7.0.517.44, Safari 5.0.2(7533.18.5), and IE 8.0.6001.18702

Description

When an element set contains only a single checkbox, adding the adding the additional constraint '[checked]' will never match the element, regardless of the actual checked state.

When only one checkbox exists,

  $('input[type=checkbox][checked])

and

  $('input[type=checkbox]').filter('[checked]')

have the same results.

If the containing set has at least one element in addition to the checkbox, the result is as expected.

Test case

Test case in 1.4.3: http://jsfiddle.net/8AuPq/3/

Check the checkbox. Line 'box [checked]' will not match the checkbox, but the other three will.
Repeating the test with 1.4.2 or IE, all lines will match the checkbox.

Change History (4)

comment:1 Changed 8 years ago by JordanClark

I forgot to mention in the ticket, but using ':checked' rather than '[checked]' doesn't exhibit this problem, so that's the work-around right there. But because the behavior is inconsistent across jQuery versions and across browsers using the same version, I thought it was worth reporting.

comment:2 Changed 8 years ago by Rick Waldron

Resolution: invalid
Status: newclosed

This is not a valid bug, please check your attributes list in the input element that you are testing, as well as your selector syntax. Here is a working example:

http://jsfiddle.net/rwaldron/8AuPq/4/

Last edited 8 years ago by Rick Waldron (previous) (diff)

comment:3 Changed 8 years ago by snover

To clarify rwaldron’s comment, checking a checkbox does not change the checked attribute, it just changes the state of the object in memory. :checked works because it inspects the DOM HTML property, not the attribute, and [checked] works in IE because Sizzle inspects DOM HTML properties when it should not. The disparity between IE and other browsers will be resolved in 1.5, at which point [checked] will function identically to all other browsers.

comment:4 Changed 8 years ago by JordanClark

Thanks for the clarification snover - that makes perfect sense.

The only part I still don't understand is why [checked] doesn't work with a single element in the matched set, but does work when there is more than one element. Shouldn't the selector work the same way regardless of the number of elements in the set?

Note: See TracTickets for help on using tickets.