This code will help you out.
<html>
<head>
<script type="text/JavaScript" src="jquery-2.0.2.min.js"></script>
<script type="text/JavaScript">
$(function(){
var defaultValue = $("#my_select").val();
$("#reset").click(function () {
$("#my_select").val(defaultValue);
});
});
</script>
</head>
<body>
<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">
<input type="button" value="reset"/>
</div>
</body>
With jquery :
$("#reset").on("click", function() {
$('#my_select').val($('#my_select').find('option[selected="selected"]').val());
}
Reset all selection fields to the default option, where the attribute selected is defined.
$("#reset").on("click", function () {
// Reset all selections fields to default option.
$('select').each( function() {
$(this).val( $(this).find("option[selected]").val() );
});
});
This works for me:
$("#reset").on("click", function () {
$("#my_select option[selected]").prop('selected', true);
}
You find for the default option that has the select attribute and you change the selection to that option.
Bind an event handler to the focus event of the select
to capture the previous value. Then set the value of the select
to the previous value when reset is clicked.
var previousValue = "";
$("#my_select").on("focus",function(){
previousValue = $(this).val();
});
$("#reset").on("click", function () {
$("#my_select").val(previousValue);
});
Working Example: http://jsfiddle.net/T8sCf/17/
You can do this way:
var preval = $('#my_select').val(); // get the def value
$("#reset").on("click", function () {
$('#my_select option[value*="' + preval + '"]').prop('selected', true);
});
checkout this fiddle
take a var
which holds the default loaded value before change event then get the option with the attribute selector of value with holds the var
value set the property to selected.