Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change selected index using jquery

Tags:

jquery

Hi need to change the selected index of a when another has been changed.. Example: i have

<select id="selector">
<option>No 1</option>
<option>No 2</option>
</select>
<select id="selected">
<option>ONE</option>
<option>TWO</option>
</select>

now i want the $selected index to change when the #selector's index is changed.. like when i change to "No 2" the other combobox will change to "TWO". pardon me for newbie question but im new to jquery ^^

like image 881
None Avatar asked Jul 03 '12 11:07

None


People also ask

How to change the selected index in jQuery?

change( function(){ var index = this. selectedIndex; $('#selected option'). eq(index). prop('selected',true); });

How can I change the selected value of a Dropdownlist using jQuery?

To achieve this feat you can use various methods, two of those methods will be explained below. Used Function: val() function: It sets or returns the value attribute of the selected elements. attr() function: It sets or returns the attributes and values of the selected elements.

How to dropdown value in jQuery?

Use the jQuery: selected selector in combination with val () method to find the selected option value in a drop-down list.


1 Answers

$('#selector').change(function() {
    var idx = this.selectedIndex;        
    $("select#selected").prop('selectedIndex', idx);  
});
like image 107
Sudhir Bastakoti Avatar answered Oct 14 '22 02:10

Sudhir Bastakoti