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 of ββthis code is to select the first non-hidden option, hiding some parameters, including the currently selected one.
Since jQuery 1.10+ $('#s1').val(''); no longer selects the first non-hidden parameter, and .val(<some proper value for the particular select box>) works fine.
Trying to use the following approaches did not help, because both selectedIndex and .first().val() consider hidden parameters:
$("#s1").prop("selectedIndex", 0);
$('#s1 option').first().prop('selected', true);
The next thing that comes to mind (also suggested by C-link ) also does not work, because the :visible selector does not work properly for selecting options.
$('#s1 option:visible').first().prop('selected', true);
You are looking for some general way (independent of knowing which specific values ββand which options were hidden) to achieve the same behavior as $('#s1').val(''); in old jQuery.
javascript jquery html
bbonev
source share