1

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...

shobia
  • 75
  • 1
  • 2
  • 9

2 Answers2

2

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>
Mr. Roshan
  • 1,777
  • 13
  • 33
  • 1
    Thanks for the timely help... I tried some other alternative solution to achieve my business needs. It works fine now. – shobia Nov 01 '18 at 13:50
  • you are welcome..!! you are also able to post your own answer to same question. – Mr. Roshan Nov 01 '18 at 13:57
-1

Try this, It will alert your selected value

<select onchange="alert(this.value)">
    <option id="1">APPLE</option>
    <option id="2">Orange</option>
</select>
mbuechmann
  • 5,413
  • 5
  • 27
  • 40
lifeisbeautiful
  • 817
  • 1
  • 8
  • 18