Ticket #3622 (closed bug: invalid)
Bug with CSS Selector E[foo] on Option elements
| Reported by: | djvirgen | Owned by: | |
|---|---|---|---|
| Priority: | minor | Milestone: | 1.3 |
| Component: | selector | Version: | 1.2.6 |
| Keywords: | Cc: | djvirgen, flesler | |
| Blocking: | Blocked by: |
Description
HTML:
<select id="theSelect">
<option>Select a color</option> <option value="blue">Blue</option> <option value="red">Red</option>
</select>
Given the above HTML, the following CSS selector incorrectly returns all 3 options:
$('#theSelect option[value]').length; 3
While the first option would submit a value when the form is submitted, it technically does not have a value attribute.
According to the W3C spec [1], the selector E[foo] should return E elements that contain a foo attribute.
Change History
comment:2 Changed 5 years ago by djvirgen
I have just tested it with the nightly build, and it is still reporting an incorrect number of matched elements.
Test code:
console.log('Number of options with a "value" attribute: ' + $('option[value]').length); incorrectly reports 3
HTML:
<select name="test">
<option>No Value Attribute</option> <option value="foo">Foo</option> <option value="bar">Bar</option>
</select>
comment:3 Changed 4 years ago by balazs.endresz
var el = $('<select><option>text</option></select>').find('option')[0];
el.value; //returns "text"
el.getAttribute('value'); //returns: null
In both 1.2.6 and 1.3b2 first the element's property is checked, which returns "text" instead of undefined even if there isn't a value attribute, so there won't be any further checks with getAttribute. Otherwise using .val() would return "text" on the element but you wouldn't be able to select it with the attribute filter.
comment:4 Changed 3 years ago by dmethvin
- Status changed from new to closed
- Resolution set to invalid
http://jsfiddle.net/dmethvin/BQQQx/
As balazs.endresz said, this is expected behavior. If jQuery didn't match against the value *property* then the selector would only match the initial value attribute.
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

Can you try with this version of jQuery ?