Kim
Kim

Reputation: 1156

Select first value in select menu with jquery

I have this Jquery code below, but when I run this it doesn't select the first value by default. How can I select the first value in this select menu with Jquery?

HTML:

   <select class="form-control" id="txtAmountDays">

Jquery:

   day = $(this).data('id');

   var options="";
   var end_period = 24;
   for(var i=day;i<=end_period;i++)
   {
     options+="<option value='"+i+"'>"+i+"</option>";
   }       
   $("#txtAmountDays").html(options); 

Upvotes: 1

Views: 237

Answers (1)

Ram
Ram

Reputation: 144659

You can set the selectedIndex property of the select element to 0 using .prop() method:

$("#txtAmountDays").html(options).prop('selectedIndex', 0); 

Or use .val() method:

$("#txtAmountDays").html(options).val(day);

Upvotes: 2

Related Questions