Bug Tracker

Modify

Ticket #3226 (closed bug: worksforme)

Opened 5 years ago

Last modified 14 months ago

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

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

comment:1 follow-up: ↓ 2 Changed 5 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 5 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 3 years ago by dmethvin

  • Component changed from core to attributes

comment:4 Changed 3 years ago by snover

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

 test case works for me.

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.