Plain and simple: Why does adding the option to the select element alters its selectedIndex and is there a way to either:
- Prevent this behavior
- Detect it
var list = document.getElementById("list");
list.selectedIndex = -1;
document.write("</br>Selected index: " + list.selectedIndex);
$(list).append('<option value="foo">foo</option>');
document.write("</br>Selected index: " + list.selectedIndex);
list.selectedIndex = -1;
document.write("</br>Selected index: " + list.selectedIndex);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="list">
</select>