This is not difficult to do if you understand that properties are different from attributes. Attributes (generally) don't change, but properties do. The selected
attribute will always remain as it is in the original HTML, while the selected
property will depend on what's happened to the element in the lifetime of the page.
So you can select the original selected element based on its selected
attribute and then set its selected
property.
document.querySelector('option[selected]').selected = true;
jsFiddle demonstrating this.
Note that this requires a modern-ish browser that supports querySelector
. This is most of them, these days, but some old browsers won't. If this is a problem, you will have to find the element using hasAttribute('selected')
.