Bug Tracker

Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#13032 closed bug (patchwelcome)

Setting select value after appending an option fails in IE9

Reported by: anonymous Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: git
Keywords: Cc:
Blocked by: Blocking:


See http://stackoverflow.com/questions/13821870/add-option-to-a-select-and-select-it


Basically, this fails (only in IE9, apparently):

    var $option = $("<option>").text("1").val("1");


The solution is to add a dummy accessor. This works:

    var $option = $("<option>").text("1").val("1");

    $("select").prepend($option).each(function() {
      this[0];  // whatever, but not e.g. nodeType

Change History (5)

comment:1 Changed 8 years ago by anonymous

Ugh, I meant prepending as the code also shows. Appending works fine...

comment:2 Changed 8 years ago by anonymous

A summary of the fixes discovered:

  • .val(1).val(1) works - not sure why.
  • .dequeue() or .delay(1) work because they call $.data, which contains the accessor that fixes the bug. (Note that .delay() doesn't affect .val(1) but it's just the accessor in its code path that does the job.)
  • Using setTimeout with a function that sets the value also solves the issue.

comment:3 Changed 8 years ago by dmethvin

Resolution: patchwelcome
Status: newclosed

Confirmed that it fails on IE9, also *not* on IE10. In the original example there is actually a text node as elem.firstChild of the select, but changing that had no effect on the bug: http://jsfiddle.net/tEUxg/8/show/

Of the proposed workarounds, any that involve async such as setTimeout are definitely out. I am not sure this is something that we should solve at the core level given its obscurity but if someone has a simple patch that doesn't cause performance issues we could give it a try.

comment:4 Changed 8 years ago by anonymous

In the setter valHook for select elements, adding elem[0]; right after the values assignment on the first line fixes the issue. I think this is a simple fix without any performance impact.

comment:5 Changed 8 years ago by dmethvin

Great, can you put together a pull request with unit tests?

Note: See TracTickets for help on using tickets.