Skip to main content

Bug Tracker

Side navigation

#13032 closed bug (patchwelcome)

Opened December 11, 2012 03:30PM UTC

Closed December 11, 2012 05:24PM UTC

Last modified December 11, 2012 06:33PM UTC

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:
Description

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

http://jsfiddle.net/tEUxg/7/

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

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

    $("select").prepend($option).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
    }).val(1);
Attachments (0)
Change History (5)

Changed December 11, 2012 03:43PM UTC by anonymous comment:1

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

Changed December 11, 2012 04:04PM UTC by anonymous comment:2

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.

Changed December 11, 2012 05:24PM UTC by dmethvin comment:3

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.

Changed December 11, 2012 06:20PM UTC by anonymous comment:4

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.

Changed December 11, 2012 06:33PM UTC by dmethvin comment:5

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