Bug Tracker

Modify

Ticket #3622 (closed bug: invalid)

Opened 5 years ago

Last modified 3 years ago

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.

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

Change History

comment:1 Changed 5 years ago by flesler

  • Cc djvirgen, flesler added

Can you try with  this version of jQuery ?

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 5 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.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.