One nice clean way is to add a data-default
attribute to the select
<select id="my_select" data-default="b">
...
</select>
An then the code is really simple:
$("#reset").on("click", function () {
var $select = $('#my_select');
$select.val($select.data('default'));
});
Live example: http://jsfiddle.net/T8sCf/7/
I have select box
<select id="my_select">
<option value="a">a</option>
<option value="b" selected="selected">b</option>
<option value="c">c</option>
</select>
<div id="reset">
reset
</div>
I have also reset button, here default (selected) value is "b", suppose I select "c" and after I need resert select box value to default, how to make this using Angular2?
You can use the data
attribute of the select
element
<select id="my_select" data-default-value="b">
<option value="a">a</option>
<option value="b" selected="selected">b</option>
<option value="c">c</option>
</select>
Your JavaScript,
$("#reset").on("click", function () {
$("#my_select").val($("#my_select").data("default-value"));
});
http://jsfiddle.net/T8sCf/10/
UPDATE
If you don't know the default selection and if you cannot update the html, add following code in the dom ready ,
$("#my_select").data("default-value",$("#my_select").val());
http://jsfiddle.net/T8sCf/24/
You can also reset the values of multiple SELECTs and then trigger the submit button.
Please see it here in action:
Live Example:
$(function(){
$("#filter").click(function(){
//alert('clicked!');
$('#name').prop('selectedIndex',0);
$('#name2').prop('selectedIndex',0);
$('#submitBtn').click();
});
});
A simple way that runs is
var myselect = $("select.SimpleAddAClass");
myselect[0].selectedIndex = 0;
myselect.selectmenu("refresh");
$("#reset").on("click", function () {
$("#my_select").val('b');//Setting the value as 'b'
});