Skip to main content

Bug Tracker

Side navigation

#3622 closed bug (invalid)

Opened November 19, 2008 12:41AM UTC

Closed November 14, 2010 02:23PM UTC

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
Blocked by: Blocking:
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.

http:www.w3.org/TR/css3-selectors/#selectors

Attachments (0)
Change History (4)

Changed November 19, 2008 10:34PM UTC by flesler comment:1

cc: → djvirgen, flesler

Can you try with this version of jQuery ?

Changed November 19, 2008 10:47PM UTC by djvirgen comment:2

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>

Changed January 11, 2009 03:09PM UTC by balazs.endresz comment:3

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.

Changed November 14, 2010 02:23PM UTC by dmethvin comment:4

resolution: → invalid
status: newclosed

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.