Skip to main content

Bug Tracker

Side navigation

#3226 closed bug (worksforme)

Opened August 06, 2008 01:58PM UTC

Closed October 13, 2010 06:42AM UTC

Last modified March 14, 2012 09:27PM UTC

val() and val(string) do not work with SELECTs in IE6

Reported by: JPLosier Owned by:
Priority: major Milestone: 1.3
Component: attributes Version: 1.2.6
Keywords: select, option, val Cc:
Blocked by: Blocking:
Description

I know; I hate IE too. But from looking at the jQuery source, it looks like you're setting or returning the VALUE of the SELECT, but that's not the DOM standard. Nice browsers like Firefox know what you mean, but IE isn't so helpful. The DOM standard uses the collection of options and the selectedIndex.

Here's some (non-jQuery) code to show what I mean:

function setSelect(strSelectID, newValue) {

var objSelect = document.getElementById(strSelectID);

if (objSelect) {

for (var i=0; i<objSelect.length; i++) {

if (objSelect.options[i].value == newValue) objSelect.selectedIndex = i;

}; for i

}; if SELECT exists

}; setSelect()

function getSelectedValue(strSelectID) {

var objSelect = document.getElementById(strSelectID);

return objSelect.options[objSelect.selectedIndex].value;

}; getSelectedValue()

My example code does not handle multi-select, but that is a matter of manipulating the SELECTED attribute of the OPTION. If you have any questions, feel free to contact me. I hope this helps make jQuery an even better product than it is now. Keep up the great work!

Attachments (0)
Change History (4)

Changed August 06, 2008 09:59PM UTC by flesler comment:1

Is this actually causing any problem ?

Can you provide a test case where something fails ?

Changed September 11, 2008 07:25PM UTC by JPLosier comment:2

Replying to [comment:1 flesler]:

Is this actually causing any problem ? Can you provide a test case where something fails ?

Yes, it causes issues with IE users if using JQuery as documented to read the value of a SELECT object.

Test case:

Make a page with a SELECT in it, such as:

<select id="gender">

<option>Male</option>

<option>Female</option>

</select>

And use jQuery:

alert($("#gender").val());

You'd expect to see "Male", but in IE, you get an error, because the DOM does not define a value property for select objects. You should update your code to return the member of the options collection property at the index provided by the selectedIndex property. I provided my quick workaround above as an example to start from.

Changed June 20, 2010 08:00PM UTC by dmethvin comment:3

component: coreattributes

Changed October 13, 2010 06:42AM UTC by snover comment:4

resolution: → worksforme
status: newclosed

test case works for me.