The following behaves differently between jQuery 1.9 and 1.10+:
<select id="s1">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
$('#s1 option[value=1]').hide();
$('#s1').val('');
The idea behind this code is to select the first non-hidden option, after hiding some options, including the currently selected one.
Since jQuery 1.10+ the $('#s1').val('');
no longer selects the first non-hidden option, while .val(<some proper value for the particular select box>)
works ok.
Trying the following approaches does not help because both selectedIndex
and .first().val()
consider the hidden options:
$("#s1").prop("selectedIndex", 0);
$('#s1 option').first().prop('selected', true);
Next thing that comes to mind (also suggested by C-link) also does not work, because the :visible
selector does not work properly for select options.
$('#s1 option:visible').first().prop('selected', true);
Looking for some generic way (not depending on knowledge of what are the particular values and what options have been hidden) to achieve the same behaviour as $('#s1').val('');
in old jQuery.