Bug Tracker

Opened 10 years ago

Closed 9 years ago

#5555 closed bug (worksforme)

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.

Change History (4)

comment:1 Changed 10 years ago by anentropic

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

comment:2 Changed 10 years ago by anentropic

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

comment:3 Changed 10 years ago by dmethvin

Component: unfiledattributes

comment:4 Changed 9 years ago by john

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

Note: See TracTickets for help on using tickets.