65

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 jquery?

$("#reset").on("click", function () {
    // What do here?
});

jsfiddle: http://jsfiddle.net/T8sCf/1/

Oto Shavadze
  • 40,603
  • 55
  • 152
  • 236
  • 2
    If you want to reset the whole form, just call the built-in JavaScript `.reset()` on the `
    ` element.
    – andyb Jun 04 '13 at 08:33
  • possible duplicate of [reset a select element](http://stackoverflow.com/questions/9141939/reset-a-select-element) – andyb Jun 04 '13 at 08:35
  • Also ` – andyb Jun 04 '13 at 08:36
  • @andyb The first element is not necessarily the default selected value – BrunoLM Jun 04 '13 at 08:36
  • @BrunoLM yes I take your point. I referenced the other question because it's very similar - "resetting a select value to default with JavaScript" – andyb Jun 04 '13 at 08:38

24 Answers24

90

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;
}
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • set your default option element with `defaultSelected` attribute and reset like this `$('#select option').removeAttr('selected');` – skorenb Aug 04 '17 at 15:57
20
$('#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>
15
$("#reset").on("click", function () {
    $("#my_select").val('b');//Setting the value as 'b'
});
웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91
13

Why not use a simple javascript function and call it on onclick event?

function reset(){
document.getElementById("my_select").selectedIndex = 1; //1 = option 2
}
Colin R. Turner
  • 1,323
  • 15
  • 24
9

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/

Chamika Sandamal
  • 23,565
  • 5
  • 63
  • 86
5
$("#reset").on("click", function () {
    $('#my_select').prop('selectedIndex',0);
});
IdemeNaHavaj
  • 2,297
  • 1
  • 12
  • 10
5
<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.

Ajit Kumar
  • 590
  • 1
  • 8
  • 17
4

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/

Jamiec
  • 133,658
  • 13
  • 134
  • 193
3

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();
    });
});
nyedidikeke
  • 6,899
  • 7
  • 44
  • 59
Chale CS
  • 353
  • 4
  • 15
3
$("#reset").on("click", function () {
    $('#my_select').val('-1');
});

http://jsfiddle.net/T8sCf/1323/

Sagar Sinha
  • 375
  • 2
  • 8
  • 26
3

If you looking to reset, try this simply:

$('element option').removeAttr('selected');

To set desire value, try like this:

$(element).val('1');
Vivek Sharma
  • 577
  • 5
  • 9
1

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/

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
1

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.

Jai
  • 74,255
  • 12
  • 74
  • 103
  • Instead of `$('#my_select option[value*="' + preval + '"]').prop('selected', true);`, you could just do `$('#my_select').val(preval)`. – Felix Kling Jun 04 '13 at 08:54
1

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>

Raghav
  • 6,893
  • 3
  • 19
  • 28
1

If using Spring forms, you may need to reset the selected to your options label. You would set your form:select id value to an empty string resets your selection to the label as the default.

<form:select path="myBeanAttribute" id="my_select">
    <form:option value="" label="--Select One--"/>
    <form:options items="${mySelectionValueList}"/>
</form:select>

 $("#reset").on("click", function () {
        $("#my_select").val("");
 });

in the case where there's a reset button. Otherwise

$("#my_select").val("");
rnyunja
  • 219
  • 3
  • 4
1

For those who are working with Bootstrap-select, you might want to use this:

$('#mySelect').selectpicker('render');
KJS
  • 1,176
  • 1
  • 13
  • 29
1

A simple way that runs is

 var myselect = $("select.SimpleAddAClass");
 myselect[0].selectedIndex = 0;
 myselect.selectmenu("refresh");
daniel_mutu
  • 163
  • 1
  • 14
1

With jquery :

$("#reset").on("click", function() {
    $('#my_select').val($('#my_select').find('option[selected="selected"]').val());
}
OlivierB
  • 11
  • 2
  • It would be worth expanding on your answer to explain what the code does. Think about new users to jQuery who have no idea what this means. – Bugs Jun 09 '17 at 13:43
1
$('#my_select').find('option:not(:first)').remove();

Call this function each time before populating the select

joash
  • 2,205
  • 2
  • 26
  • 31
0

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.

Tomás
  • 3,501
  • 3
  • 21
  • 38
0

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() );
    });
});
Sjoerd Linders
  • 447
  • 5
  • 8
0

I was trying to resolve it like the other answers unfortunately, I didn't get a right way to do it, once I tried as I write below:

$('#<%=ddID.ClientID %>').get(0).selectedIndex = 0;

this code works for me, I hope that will be useful for you guys.

Best Regards.

Samuel Alvarado.

0

You can try

$('select')[0].value = "default vaule"

function reset(){
  $('select')[0].value="default value"
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select>
  <option value="default value" >Default</option>
  <option value="1" >Value 1</option>
  <option value="2" >Value 2</option>
  <option value="3" >Value 3</option>
</select>
<button onclick="reset()" >Click</button>
-3

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?