given that you have a simple selection box. You can use val() to set the parameters.
<select id="selectId"> <option value="0">Default Value</option> <option value="1">1</option> <option value="2">2</option> </select> <input type="button" id="buttonID" value="change"/>
try it
$('#buttonID').click(function(){ $('#selectId').val('0');
However, if you use disabled by default, you will need to work to complete the task. a little like this:
$(document).on('click', '#buttonID', function(e) { var d = $('#selectId option:disabled').prop('disabled', ''); $('#selectId').val(0); d.prop('disabled', 'disabled'); });
Remember that you can change the value of $('#selectId').val(0); to meet your own needs. For example, if the third option is your default and has the value "bob", you can say $('#selectId').val('bob');
the answer I provided is simply the easiest solution ... @ SpYk3HH changed it if you disabled your default option .... and yes @Felix Kling already answered this question in a previous post here , so take a look. thanks anyway guys .. :)
Jquery-based attribute solution for cross-compatible
To continue with FelixKling's solution, here is the full version of jQuery using .attr, which extracts the original attributes and not the “changed properties” from .prop.
$(document).on('click', '#buttonID', function(e) { $('#selectID option').filter(function(i){ return this.hasAttribute('selected') }).prop('selected', 'selected') });
To further strengthen the solution, try combos from 2 above. Thus, regardless of the HTML format, you are tied to reset by default. I mean, maybe you do not have a selection box with a default value set, but option 1 is the default value at startup. Meanwhile, other selects have a default set. The following will take care of all the problems at the same time.
// simply a jQuery 1.7+ way of delegating events to any Element match selector $(document).on('click', 'button', function(e) { // This both selects the first option of a select as well as looks for an option that might have had a default setting var opt = $('select').val(0).children('option').filter(function(i){ return this.hasAttribute('selected') }); // if opt.length is 0, this will do nothing and option 1 is already set, else this will set this default option to selected opt.prop('selected', 'selected'); // if you have an expected event tied to the select "change" event, you might fire it here, like so: $('select').change(); });
w / Out Comments, beautiful and short
$(document).on('click', 'button', function(e) { var opt = $('select').val(0).children('option').filter(function(i){ return this.hasAttribute('selected') }); opt.prop('selected', 'selected'); });