Why does this code would work fine in jQuery 1.5 and not in jQuery 1.6? Was there a known change to how jquery creates DOM nodes between these two versions?
var birth = new Date(),
current = new Date().getFullYear() - 13,
year = $('select#year'),
i = 0;
birth.setFullYear(1992, 10, 3);
while (i < 48) {
var option = $('<option>', {
value: current - i,
text: current - i,
selected: (current - i === birth.getFullYear()) ? 'selected' : ''
});
year.append(option);
i++;
}
See this working jsfiddle using 1.5 and this non-working jsfiddle using 1.6. The first only selects one option, but the second adds selected="selected" to every option.
Note I’ve only tested this in Chrome at this point.
will generate either
or
In both of these cases, the browser considers
selectedset. That’s becauseselectedis a Boolean; Its mere presence signals that it should be selected.You should use
prop, which was introduced in jQuery 1.6:http://jsfiddle.net/Z7umc/3/
Or, if you prefer to have it all within your object: