I have hit a weird case where, to the best of my knowledge, two things that should act the same behave quite differently.
If I have two select menus on a page, one static menu hardcoded in the HTML and one appended to the body at runtime with JQuery. I then disable the first option in both select menus. When displayed both menus have their first options disabled as expected, however the dynamically appended menu has automatically set the value to the second option while the static menu still has the first selected.
http://jsfiddle.net/hm3xgkLg/1/
HTML:
<select class="dropMenu">
<option value="1">First</option>
<option value="2">Second</option>
<option value="3">Third</option>
<option value="4">fourth</option>
</select>
Javascript:
var arr = [
{val : 1, text: 'One'},
{val : 2, text: 'Two'},
{val : 3, text: 'Three'},
{val : 4, text: 'Four'}
];
var sel = $('<select class="dropMenu">').appendTo('body');
$(arr).each(function() {
sel.append($("<option>").attr('value',this.val).text(this.text));
});
$('.dropMenu option:nth-child(1)').attr('disabled', 'disabled');
Why are these two seemingly identical select menus behaving differently? I would like both to act like the static menu (keep 1st value selected) is that possible?
It should also be noted I tried wrapping the disable function in $(document).ready to rule out an issue of the menu not being rendered yet but there was no change. I also tried having two distinct classes with two separate calls to disable the options to make sure its wasn't clashing somehow with no change.