shobia
shobia

Reputation: 75

How to set an id to option in HTML.DropDownList

I have an HTML.DropDownList where I can set datavaluefield and datatextfield like the following,

<select>
<option value="Fruit">APPLE</option>
</select>

Even, I can able to set the ID to dropdown list using html attributes but Since I need to set the 'Id' to the option like following,

 <select>
 <option ID='1' value="Fruit">APPLE</option>
 </select>

Can anyone share the idea on it...

Upvotes: 1

Views: 14903

Answers (2)

lifeisbeautiful
lifeisbeautiful

Reputation: 846

Try this, It will alert your selected value

<select onchange="alert(this.value)">
    <option id="1">APPLE</option>
    <option id="2">Orange</option>
</select>

Upvotes: -1

Mr. Roshan
Mr. Roshan

Reputation: 1805

You can use the id attribute to set the ID to the option tag and use this.value into the onchange callback with select element.

Below is the working example

document.getElementById("select1").onchange = function() {
    if (this.value == 'Fruit') {
        var optionID=document.getElementById('1');
        alert(optionID.value);

    }
    if (this.value == 'Flower') {
        var optionID=document.getElementById('2');
        alert(optionID.value);

    }
}
<select name="select01" id="select1" onchange="handleSelect()">
  <option value="Fruit" id="1">Apple</option>
  <option value="Flower" id="2">Rose</option>
</select>

Upvotes: 2

Related Questions