78

I have

<select id="baba">
<option>select something</option>
<option value="1">something 1</option>
<option value=2">something 2</option>
</select>

Using jQuery, what would be the easiest (less to write) way to reset the select so the first option is selected?

Itay Moav -Malimovka
  • 50,983
  • 60
  • 187
  • 270

14 Answers14

192

Try this. This will work. $('#baba').prop('selectedIndex',0);

Check here http://jsfiddle.net/bibin_v/R4s3U/

Bibin Velayudhan
  • 2,875
  • 1
  • 20
  • 32
31

In your case (and in most use cases I have seen), all you need is:

$("#baba").val("");

Demo.

Eaten by a Grue
  • 19,208
  • 10
  • 77
  • 99
karim79
  • 334,458
  • 66
  • 409
  • 405
  • 5
    if the select element does not have an option with `value=""` then one will be created in newer versions of jquery – Eaten by a Grue Jan 13 '17 at 17:50
  • I stumbled upon the exact issue. My code suddenly stopped working and I figured out that upgrading jquery from 1x to 2x was the reason. Hence it is dangerous to use this code. One small correction to @billynoah 's comment - A new option tag won't be created, but the selected value will be null. On clicking the select, we will be able to select from the original list of options only. Better use some other solution as it will fail in jquery 2+. – Ridhuvarshan Jul 31 '18 at 13:07
  • 2
    Down voting. This does not work with newer versions of jquery and has a bad side-effect of creating a new option and you will have to fix this if you upgrade. – splashout Oct 22 '19 at 18:55
13
$('#baba option:first').prop('selected',true);

Nowadays you best use .prop(): http://api.jquery.com/prop/

Roko C. Buljan
  • 180,066
  • 36
  • 283
  • 292
7
$('#baba').prop('selectedIndex',-1);
Andrea
  • 11,073
  • 17
  • 60
  • 64
serviom
  • 101
  • 1
  • 6
7

if none of those solutions didn't work for you, try adding after

.trigger( "change" );

ex.

$("#baba").val("").trigger( "change" );

or

$("#baba").val(false).trigger( "change" );

or

$("#baba option").prop("selected", false).trigger( "change" );

or

$('#baba').prop('selectedIndex',-1).trigger( "change" );

or

$('#baba option:first').prop('selected',true).trigger( "change" );
Moode Osman
  • 1,110
  • 12
  • 13
  • Most comprehensive answer +1, but should give a short explanation about each option. They vary a little. – JoePC May 13 '21 at 17:51
6

Reset single select field to default option.

<select id="name">
    <option>select something</option>
    <option value="1" >something 1</option>
    <option value="2" selected="selected" >Default option</option>
</select>
<script>
    $('name').val( $('name').find("option[selected]").val() );
</script>


Or if you want to reset all form fields to the default option:

<script>
    $('select').each( function() {
        $(this).val( $(this).find("option[selected]").val() );
    });
</script>
Sjoerd Linders
  • 427
  • 5
  • 8
4

Edit: Old fashioned way,

document.getElementById('baba').selectedIndex = 0;

Try below and it should work for your case,

$('#baba option:first').prop('selected', true);

DEMO

Selvakumar Arumugam
  • 78,145
  • 14
  • 119
  • 133
2
$('#baba option:first').attr('selected',true);
Thulasiram
  • 8,258
  • 8
  • 45
  • 52
1

This does the trick, and works for any select.

$('#baba').val($(this).find('option:first').val());
ilyes kooli
  • 11,766
  • 13
  • 49
  • 79
1

The best javascript solution I've found is this

elm.options[0].selected="selected";
Sebastian Td
  • 134
  • 1
  • 7
1

If you don't want to use the first option (in case the field is hidden or something) then the following jQuery code is enough:

$(document).ready(function(){
    $('#but').click(function(){
        $('#baba').val(false);
    })
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<select id="baba">
<option>select something</option>
<option value="1">something 1</option>
<option value=2">something 2</option>
</select>
    
    <input type="button" id="but" value="click">
Junaid Anwar
  • 745
  • 6
  • 20
1

This function should work for all types of select (multiple, select, select2):

$.fn.Reset_List_To_Default_Value = function()
{
    $.each($(this), function(index, el) {
        var Founded = false;

        $(this).find('option').each(function(i, opt) {
            if(opt.defaultSelected){
                opt.selected = true;
                Founded = true;
            }
        });

        if(!Founded)
        {
            if($(this).attr('multiple'))
            {
                $(this).val([]);
            }
            else
            {
                $(this).val("");
            }
        }
        $(this).trigger('change');
    });
}

To use it just call:

$('select').Reset_List_To_Default_Value();
Mohamad Hamouday
  • 1,446
  • 16
  • 17
0

I believe:

$("select option:first-child").attr("selected", "selected");
raydowe
  • 1,235
  • 1
  • 16
  • 31
0

This works a little differently from other answers in that it unselects all options:

$("#baba option").prop("selected", false);

(Borrowed from https://stackoverflow.com/a/11098981/1048376)

splashout
  • 499
  • 5
  • 10