How to reset a select element with jQuery

JqueryHtml

Jquery Problem Overview


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?

Jquery Solutions


Solution 1 - Jquery

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

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

Solution 2 - Jquery

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

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

Demo.

Solution 3 - Jquery

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

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

Solution 4 - Jquery

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

Solution 5 - Jquery

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" );

Solution 6 - Jquery

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>

Solution 7 - Jquery

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

Solution 8 - Jquery

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

Solution 9 - Jquery

This does the trick, and works for any select.

$('#baba').val($(this).find('option:first').val());

Solution 10 - Jquery

The best javascript solution I've found is this

elm.options[0].selected="selected";

Solution 11 - Jquery

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">

Solution 12 - Jquery

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();

Solution 13 - Jquery

I believe:

$("select option:first-child").attr("selected", "selected");

Solution 14 - Jquery

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionItay Moav -MalimovkaView Question on Stackoverflow
Solution 1 - JqueryBibin VelayudhanView Answer on Stackoverflow
Solution 2 - Jquerykarim79View Answer on Stackoverflow
Solution 3 - JqueryRoko C. BuljanView Answer on Stackoverflow
Solution 4 - JqueryserviomView Answer on Stackoverflow
Solution 5 - JqueryMoode OsmanView Answer on Stackoverflow
Solution 6 - JquerySjoerd LindersView Answer on Stackoverflow
Solution 7 - JquerySelvakumar ArumugamView Answer on Stackoverflow
Solution 8 - JqueryThulasiramView Answer on Stackoverflow
Solution 9 - Jqueryilyes kooliView Answer on Stackoverflow
Solution 10 - JquerySebastian TdView Answer on Stackoverflow
Solution 11 - JqueryJunaid AnwarView Answer on Stackoverflow
Solution 12 - JqueryMohamad HamoudayView Answer on Stackoverflow
Solution 13 - JqueryraydoweView Answer on Stackoverflow
Solution 14 - JquerysplashoutView Answer on Stackoverflow