How to make the first option of <select> selected with jQuery

JquerySelect

Jquery Problem Overview


How do I make the first option of

Jquery Solutions


Solution 1 - Jquery

$("#target").val($("#target option:first").val());

Solution 2 - Jquery

You can try this

$("#target").prop("selectedIndex", 0);

Solution 3 - Jquery

// remove "selected" from any options that might already be selected
$('#target option[selected="selected"]').each(
    function() {
        $(this).removeAttr('selected');
    }
);


// mark the first option as selected
$("#target option:first").attr('selected','selected');

Solution 4 - Jquery

When you use

$("#target").val($("#target option:first").val());

this will not work in Chrome and Safari if the first option value is null.

I prefer

$("#target option:first").attr('selected','selected');

because it can work in all browsers.

Solution 5 - Jquery

Changing the value of the select input or adjusting the selected attribute can overwrite the default selectedOptions property of the DOM element, resulting in an element that may not reset properly in a form that has had the reset event called.

Use jQuery's prop method to clear and set the option needed:

$("#target option:selected").prop("selected", false);
$("#target option:first").prop("selected", "selected");

Solution 6 - Jquery

$("#target")[0].selectedIndex = 0;

Solution 7 - Jquery

One subtle point I think I've discovered about the top voted answers is that even though they correctly change the selected value, they do not update the box on Chrome desktop, so this may not be the case everywhere).

Solution 8 - Jquery

If you have disabled options, you may add not([disabled]) to prevent selecting them which results into the following:

$("#target option:not([disabled]):first").attr('selected','selected')

Solution 9 - Jquery

Another way to reset the values (for multiple selected elements) could be this:

$("selector").each(function(){

    /*Perform any check and validation if needed for each item */

    /*Use "this" to handle the element in javascript or "$(this)" to handle the element with jquery */

    this.selectedIndex=0;

});

Solution 10 - Jquery

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

Solution 11 - Jquery

This worked for me!

$("#target").prop("selectedIndex", 0);

Solution 12 - Jquery

I've found that just setting attr selected doesn't work if there's already a selected attribute. The code I use now will first unset the selected attribute, then select the first option.

$('#target').removeAttr('selected').find('option:first').attr('selected', 'selected');

Solution 13 - Jquery

Simple like that:

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

Solution 14 - Jquery

Here is how I would do it:

$("#target option")
    .removeAttr('selected')
    .find(':first')     // You can also use .find('[value=MyVal]')
        .attr('selected','selected');

Solution 15 - Jquery

Although the each function probably isn't necessary ...

$('select').each(function(){
    $(this).find('option:first').prop('selected', 'selected');
});

works for me.

Solution 16 - Jquery

It only worked for me using a trigger('change') at the end, like this:

$("#target option:first").attr('selected','selected').trigger('change');

Solution 17 - Jquery

If you are going to use the first option as a default like

<select>
    <option value="">Please select an option below</option>
    ...

then you can just use:

$('select').val('');

It is nice and simple.

Solution 18 - Jquery

For me it only worked when I added the following code:

.change();

For me it only worked when I added the following code: As I wanted to "reset" the form, that is, select all the first options of all the selects of the form, I used the following code:

$('form').find('select').each(function(){ $(this).val($("select option:first").val()); $(this).change(); });

Solution 19 - Jquery

Use:

$("#selectbox option:first").val()

Please find the working simple in this JSFiddle.

Solution 20 - Jquery

On the back of James Lee Baker's reply, I prefer this solution as it removes the reliance on browser support for :first :selected ...

$('#target').children().prop('selected', false);
$($('#target').children()[0]).prop('selected', 'selected');

Solution 21 - Jquery

For me, it only worked when I added the following line of code

$('#target').val($('#target').find("option:first").val());

Solution 22 - Jquery

$("#target").val(null);

worked fine in chrome

Solution 23 - Jquery

If you're storing the jQuery object of the select element:

var jQuerySelectObject = $("...");

...

jQuerySelectObject.val(jQuerySelectObject.children().eq(0).val());

Solution 24 - Jquery

Check this best approach using jQuery with ECMAScript 6:

$('select').each((i, item) => {
  var $item = $(item);
  $item.val($item.find('option:first').val()); 
});

Solution 25 - Jquery

You can select any option from dropdown by using it.

// 'N' can by any number of option.  // e.g.,  N=1 for first option
$("#DropDownId").val($("#DropDownId option:eq(N-1)").val()); 

Solution 26 - Jquery

$('select#id').val($('#id option')[index].value)

Replace the id with particular select tag id and index with particular element you want to select.

i.e.

<select class="input-field" multiple="multiple" id="ddlState" name="ddlState">
                                        <option value="AB">AB</option>
                                        <option value="AK">AK</option>
                                        <option value="AL">AL</option>
</select>

So here for first element selection I will use following code :

$('select#ddlState').val($('#ddlState option')[0].value)

Solution 27 - Jquery

var firstItem = $("#interest-type").prop("selectedIndex", 0).val();

console.log(firstItem)

Solution 28 - Jquery

Use:

alert($( "#target option:first" ).text());

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
QuestionomgView Question on Stackoverflow
Solution 1 - JqueryDavid AndresView Answer on Stackoverflow
Solution 2 - JqueryEsbenView Answer on Stackoverflow
Solution 3 - Jqueryuser149513View Answer on Stackoverflow
Solution 4 - JqueryJimmy HuangView Answer on Stackoverflow
Solution 5 - JqueryJames Lee BakerView Answer on Stackoverflow
Solution 6 - JquerydanactiveView Answer on Stackoverflow
Solution 7 - JqueryBradley BossardView Answer on Stackoverflow
Solution 8 - JqueryMelanieView Answer on Stackoverflow
Solution 9 - JqueryNick PetropoulosView Answer on Stackoverflow
Solution 10 - JqueryFFFFFFView Answer on Stackoverflow
Solution 11 - JqueryArchana KamathView Answer on Stackoverflow
Solution 12 - JqueryFrancis LewisView Answer on Stackoverflow
Solution 13 - JqueryRamon SchmidtView Answer on Stackoverflow
Solution 14 - JqueryFabrizioView Answer on Stackoverflow
Solution 15 - JqueryDarth JonView Answer on Stackoverflow
Solution 16 - JqueryPablo S G PachecoView Answer on Stackoverflow
Solution 17 - JqueryGary - ITBView Answer on Stackoverflow
Solution 18 - JqueryJean DouglasView Answer on Stackoverflow
Solution 19 - JqueryChetanView Answer on Stackoverflow
Solution 20 - JqueryMatthew ScottView Answer on Stackoverflow
Solution 21 - JqueryMuhammad QasimView Answer on Stackoverflow
Solution 22 - JqueryKrishna MurthyView Answer on Stackoverflow
Solution 23 - JqueryDanielView Answer on Stackoverflow
Solution 24 - JqueryjdnichollscView Answer on Stackoverflow
Solution 25 - JqueryArsman AhmadView Answer on Stackoverflow
Solution 26 - JqueryHitesh KalwaniView Answer on Stackoverflow
Solution 27 - JquerydevlarriView Answer on Stackoverflow
Solution 28 - JqueryGobsYatsView Answer on Stackoverflow