The following HTML Markup consists of an HTML DropDownList (DropDown) control and a Button. When the Button is clicked, the Reset JavaScript function is executed. Inside this function, the SelectedIndex property of the DropDownList is set to 0 (First Item).
If you want to reset the whole form, just call the built-in JavaScript . reset() on the <form> element. Also <select> has a defaultSelected which could be used in the function to reset if you just want a single element reset - see stackoverflow.com/questions/2348042/…
By clicking on the clear icon which is shown in DropDownList element, you can clear the selected item in DropDownList through interaction.
The option tag contains the value that would be used when selected. The default value of the select element can be set by using the 'selected' attribute on the required option. This is a boolean attribute. The option that is having the 'selected' attribute will be displayed by default on the dropdown list.
You can make use of the defaultSelected
property of an option element:
Contains the initial value of the
selected
HTML attribute, indicating whether the option is selected by default or not.
So, the DOM interface already keeps track which option was selected initially.
$("#reset").on("click", function () {
$('#my_select option').prop('selected', function() {
return this.defaultSelected;
});
});
DEMO
This would even work for multi-select elements.
If you don't want to iterate over all options, but "break" after you found the originally selected one, you can use .each
instead:
$('#my_select option').each(function () {
if (this.defaultSelected) {
this.selected = true;
return false;
}
});
Without jQuery:
var options = document.querySelectorAll('#my_select option');
for (var i = 0, l = options.length; i < l; i++) {
options[i].selected = options[i].defaultSelected;
}
$('#my_select').get(0).selectedIndex = 1;
But, In my opinion, the better way is using HTML only (with <input type="reset" />
):
<form>
<select id="my_select">
<option value="a">a</option>
<option value="b" selected="selected">b</option>
<option value="c">c</option>
</select>
<input type="reset" value="reset" />
</form>
$("#reset").on("click", function () {
$("#my_select").val('b');//Setting the value as 'b'
});
Why not use a simple javascript function and call it on onclick event?
function reset(){
document.getElementById("my_select").selectedIndex = 1; //1 = option 2
}
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/
$("#reset").on("click", function () {
$('#my_select').prop('selectedIndex',0);
});
<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>
$("#reset").on("click", function () {
$('#my_select').prop('selectedIndex',0);
});
This is a simple way for your question. only one line answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With