user2991584
user2991584

Reputation:

Selected using option ID

<select id="editSkillList">  
     <option id="0">Select</option>
     <option id="8">text1</option>
     <option id="5">text2</option>
     <option id="4">text3</option>     
</select><br><br>

Javascript code

document.getElementById("editSkillList").selectedIndex="2";

But i want set selected using option id.Is it posible? then please help me

Upvotes: 0

Views: 59

Answers (3)

Govinda Rajbhar
Govinda Rajbhar

Reputation: 3034

you can select your option using below code.

Javascript

document.getElementById("editSkillList").value = document.getElementById('4').value;

or you can use this code also.

var editSkillList = document.getElementById("editSkillList");

editSkillList.options[editSkillList.selectedIndex].value;

i hope it will help you.

Upvotes: 0

techfoobar
techfoobar

Reputation: 66693

You can do:

document.getElementById('5').selected = true;

Replace '5' with the id of the option you need to select.

Upvotes: 1

adeneo
adeneo

Reputation: 318352

You can just set the selects value to the text you get from the option after targeting the ID

document.getElementById("editSkillList").value = document.getElementById('4').innerText;

FIDDLE

EDIT:

If the options have a value, you just set the selects value to one of the options value

document.getElementById("editSkillList").value = document.getElementById('4').value;

Upvotes: 1

Related Questions