Search code examples
javascripthtmldomdrop-down-menu

Get selected option text with JavaScript


I have a dropdown list like this:

<select id="box1">
<option value="98">dog</option>
<option value="7122">cat</option>
<option value="142">bird</option>
</select>

How can I get the actual option text rather than the value using JavaScript? I can get the value with something like:

<select id="box1" onChange="myNewFunction(this.selectedIndex);" >

But rather than 7122 I want cat.


Solution

  • Try options

    function myNewFunction(sel) {
      alert(sel.options[sel.selectedIndex].text);
    }
    <select id="box1" onChange="myNewFunction(this);">
      <option value="98">dog</option>
      <option value="7122">cat</option>
      <option value="142">bird</option>
    </select>