Skip to main content

Bug Tracker

Side navigation

#3104 closed bug (worksforme)

Opened June 30, 2008 03:10PM UTC

Closed September 28, 2010 09:58PM UTC

setting a value on a select element ambiguous

Reported by: Borre Owned by:
Priority: major Milestone: 1.4.3
Component: attributes Version: 1.4.2
Keywords: Cc:
Blocked by: Blocking:
Description

given a select box:

<select id="test_dropdown">
  <option value="1">somevalue</option>
  <option value="2">1</option>
</select>

and given the statement

$('#test_dropdown').val('1');

the second option will be selected. This seems to be unwanted behaviour (in my opinion, an option with a value attribute should have priority). The erroneous code seems to be located in jquery-1.2.6.js on line 419:

jQuery( "option", this ).each(function(){
	this.selected = (jQuery.inArray( this.value, values ) >= 0 ||
	jQuery.inArray( this.text, values ) >= 0);
});
Attachments (0)
Change History (5)

Changed September 15, 2008 09:28PM UTC by laurenty comment:1

I confirm this unwanted behavior.

As per W3 standard regarding the 'value' attribute of OPTION:

"This attribute specifies the initial value of the control. If this attribute is not set, the initial value is set to the contents of the OPTION element."

Changed January 28, 2009 02:12PM UTC by john comment:2

resolution: → fixed
status: newclosed

It sounds like this was fixed in jQuery 1.3.

Changed January 28, 2009 02:34PM UTC by Borre comment:3

resolution: fixed
status: closedreopened

I'm sorry to say it was not. The problem still exists.

The solution that works for me is replacing jquery-1.3.1.js, line 462-465:

jQuery( "option", this ).each(function(){
	this.selected = (jQuery.inArray( this.value, values ) >= 0 ||
	jQuery.inArray( this.text, values ) >= 0);
});

with:

var selectedFound = false;
jQuery( "option", this ).each(function(){
	selectedFound = selectedFound || (this.selected = (jQuery.inArray( this.value, values ) >= 0));
});

if(!selectedFound){
	jQuery( "option", this ).each(function(){
		this.selected = (jQuery.inArray( this.text, values ) >= 0);
	});					
}

Changed June 20, 2010 07:14PM UTC by dmethvin comment:4

component: coreattributes

Changed September 28, 2010 09:58PM UTC by john comment:5

milestone: 1.31.4.3
resolution: → worksforme
status: reopenedclosed
version: 1.2.61.4.2

This was resolved in 1.4. More tests here to confirm: http://github.com/jquery/jquery/commit/879799fe955f01b85b59fd8a0096d415fc48df03