I’ve tried about 10+ different variations trying to do the same thing – make the last element of a listbox selected programmatically. This works for Google Chrome and the latest Mozilla Firefox, but does not work for IE9 UNLESS i add an “alert(”)” at the end, in which case the last element of the listbox does get selected when i press the alert’s “ok” button.
$(listboxSelectorParam).children().filter(":last-child").attr("selected", "selected");
$(listboxSelectorParam).children().filter(":last").attr("selected", "selected");
$(listboxSelectorParam).children().filter(":last-child").attr("selected", true);
$(listboxSelectorParam).children().filter(":last").attr("selected", true);
$(listboxSelectorParam + " option:last").attr("selected", "selected);
$(listboxSelectorParam + " option:last-child").attr("selected", "selected);
.. the list goes on
How to make this work for IE9 ?
Edit: Solved it
Had to unselect the previous selection:
$(listboxSelectorParam + " option:selected").attr("selected", false);
$(listboxSelectorParam).children().filter(":last-child").attr("selected", "selected");
The soluton was to deselect the previous selection of the listbox.