Bug Tracker

Opened 11 years ago

Closed 9 years ago

Last modified 8 years ago

#3226 closed bug (worksforme)

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!

Change History (4)

comment:1 Changed 11 years ago by flesler

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

comment:2 in reply to:  1 Changed 11 years ago by JPLosier

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

comment:3 Changed 9 years ago by dmethvin

Component: coreattributes

comment:4 Changed 9 years ago by snover

Resolution: worksforme
Status: newclosed

test case works for me.

Note: See TracTickets for help on using tickets.