Bug Tracker

Ticket #3104 (closed bug: worksforme)

Opened 6 years ago

Last modified 4 years ago

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

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);
});

Change History

comment:1 Changed 6 years ago by laurenty

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

comment:2 Changed 6 years ago by john

  • Status changed from new to closed
  • Resolution set to fixed

It sounds like this was fixed in jQuery 1.3.

comment:3 Changed 6 years ago by Borre

  • Status changed from closed to reopened
  • Resolution fixed deleted

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);
	});					
}

comment:4 Changed 4 years ago by dmethvin

  • Component changed from core to attributes

comment:5 Changed 4 years ago by john

  • Status changed from reopened to closed
  • Version changed from 1.2.6 to 1.4.2
  • Resolution set to worksforme
  • Milestone changed from 1.3 to 1.4.3

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

Note: See TracTickets for help on using tickets.