Skip to main content

Bug Tracker

Side navigation

#5555 closed bug (worksforme)

Opened November 26, 2009 01:38PM UTC

Closed September 28, 2010 09:58PM UTC

using val(val) to set selected option of select list selects wrong option

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

I have a select list like:

<select name="sizes" id="mysel">

<option value="3">3</option>

<option value="4">3.5</option>

<option value="5">4</option>

<option value="6">4.5</option>

<option value="7">5</option>

</select>

I want to set the selected option by value (which correspond to ids in the db):

$('#mysel').val(4)

This selects the option with the text label 4, whose value is actually 5.

This is surely incorrect behaviour.

Attachments (0)
Change History (4)

Changed November 26, 2009 01:40PM UTC by anentropic comment:1

..especially since $('#mysel').val() returns the value, *not* the label... so the val method of select lists works differently depending on whether you're getting or setting!

Changed November 26, 2009 01:47PM UTC by anentropic comment:2

Well, I just read a mailing list post explaining what jQuery is doing here:

http://osdir.com/ml/jQuery/2009-05/msg00584.html

Apparently it's even worse... the behaviour is pretty much unpredictable, it could return value OR label depending on the structure of your list. It seems the method has been optimised for speed ahead of utility and common sense...

Changed June 13, 2010 07:00PM UTC by dmethvin comment:3

component: unfiledattributes

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

milestone: 1.41.4.3
resolution: → worksforme
status: newclosed
version: 1.3.21.4.2

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