Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jquery

select

How do I make the first option of selected with jQuery?

<select id="target">   <option value="1">...</option>   <option value="2">...</option> </select> 
like image 833
omg Avatar asked Sep 12 '09 04:09

omg


People also ask

How do I get the first select option in jQuery?

Select first element of <select> element using JQuery selector. Use . prop() property to get the access to properties of that particular element. Change the selectedIndex property to 0 to get the access to the first element.

How do you select a particular option in a select element in jQuery?

Syntax of jQuery Select Option$("selector option: selected"); The jQuery select option is used to display selected content in the option tag. text syntax is below: var variableValue = $("selector option: selected").

How do I change selected option in select tag?

In order to change the selected option by the value attribute, all we have to do is change the value property of the <select> element. The select box will then update itself to reflect the state of this property. This is the easiest and most straightforward way of updating a select box state.


2 Answers

$("#target").val($("#target option:first").val()); 
like image 142
David Andres Avatar answered Dec 08 '22 13:12

David Andres


You can try this

$("#target").prop("selectedIndex", 0); 
like image 37
Esben Avatar answered Dec 08 '22 12:12

Esben