Opened 11 years ago
Closed 10 years ago
#7128 closed bug (fixed)
attribute selector is inconsistent between qSA and Sizzle due to use of DOM properties
| Reported by: | adipose | Owned by: | timmywil |
|---|---|---|---|
| Priority: | blocker | Milestone: | 1.7 |
| Component: | selector | Version: | 1.4.3 |
| Keywords: | equal selector | Cc: | |
| Blocked by: | Blocking: | #9261 |
Description
If I set an input value with javascript, the jquery selector does not select it unless I use an incorrectly formatted selector.
This occurs in Chrome 7.0.517.24, Firefox 3.6.10, Firefox 4.0b6, Opera 10.62 and Safari 4.0 (Win32).
IE8, however, does work properly.
Correct selector: $("[value=a]") does not work. Incorrect selector: $("&[value=a]") does work. $("@[value=a]") does work. etc.
See the attached example. It works at first, but after the javascript changes the value, it gets wrong results (except in IE8).
Attachments (2)
Change History (34)
Changed 11 years ago by
comment:1 Changed 11 years ago by
Changed 11 years ago by
comment:2 Changed 11 years ago by
| Priority: | high → low |
|---|---|
| Summary: | Jquery selector broken on values set with javascript? → value attribute selector is inconsistent between qSA and Sizzle due to use of DOM properties |
The value attribute in HTML actually corresponds to the defaultValue DOM property.
Presumably, the reason the “incorrect” selectors work is because the browser’s native querySelectorAll function fails (due to their invalidity) and jQuery falls back to using Sizzle’s slow path.
The real bug here sounds like the fact that Sizzle inspects DOM 2 HTML properties instead only using getAttribute, and perhaps also that IE8 is doing something else it shouldn’t be.
comment:3 Changed 11 years ago by
"The value attribute in HTML actually corresponds to the defaultValue DOM property."
Is this to say, a jquery selector that inspects value will only ever look at the defaultValue DOM property?
When using .filter(), one can select based on value and not just defaultValue. Is this also a bug?
What is the proper way to select by value if value has been changed?
comment:4 Changed 11 years ago by
The jQuery attribute selector [value="foo"] should always inspect the HTML attribute value (which corresponds to the DOM property defaultValue), not the DOM property value which corresponds to the user- or script-modified state of the control.
To select by the current value, you would do something like:
$('input:text').filter(function () { return this.value === "foo"; })
comment:5 Changed 11 years ago by
Thank you, I did not realize this, before.
However, I would like to point out that
$('input').filter("[value=foo]")
Works. Is this supposed to work, or will it break once the other bug is corrected?
comment:6 Changed 11 years ago by
| Status: | new → open |
|---|
comment:8 Changed 11 years ago by
| Blocking: | 6954 added |
|---|
comment:6 Changed 11 years ago by
| Blocking: | 6954 removed |
|---|
comment:10 Changed 11 years ago by
| Milestone: | 1.4.3 → 1.next |
|---|---|
| Priority: | low → blocker |
| Version: | 1.4.2 → 1.4.3 |
Many more duplicates are listed in #3399.
comment:11 Changed 11 years ago by
| Summary: | value attribute selector is inconsistent between qSA and Sizzle due to use of DOM properties → attribute selector is inconsistent between qSA and Sizzle due to use of DOM properties |
|---|
comment:14 Changed 11 years ago by
| Blocking: | 7485 added |
|---|
(In #7485) This is something on the roadmap for 1.5. .attr will be rewritten along with Sizzle to stop matching on the DOM properties, so this will all be happily consistent again.
comment:15 Changed 11 years ago by
| Milestone: | 1.5 → 1.6 |
|---|
This is getting bumped to 1.6 when the $.attr rewrite happens.
comment:16 Changed 11 years ago by
| Blocking: | 7485 removed |
|---|
comment:18 Changed 11 years ago by
| Milestone: | 1.6 → 1.next |
|---|
comment:22 Changed 11 years ago by
| Blocking: | 9261 added |
|---|
comment:23 Changed 11 years ago by
| Milestone: | 1.next → 1.7 |
|---|
comment:25 Changed 11 years ago by
| Owner: | set to timmywil |
|---|---|
| Status: | open → assigned |
comment:32 Changed 10 years ago by
| Resolution: | → fixed |
|---|---|
| Status: | assigned → closed |

Added file as html