Link to home
Create AccountLog in
Avatar of earwig75
earwig75

asked on

How do I get selected name from a SELECT (drop downlist) with JavaScript?

I use code similar to the below to get the selected value of a drop down list. Can someone tell me how I would get the selected name from the control also? Thank you.

      function myFunction(){
         var elem = document.getElementById('selBoxOK');
         var selValue = elem.options[ elem.selectedIndex ].value;
         }
Avatar of Alexandre Simões
Alexandre Simões
Flag of Switzerland image

var elem = document.getElementById('selBoxOK');
var selName = elem.name;

Open in new window

Options don't have a name, so what you want is the name of the select itself.

Also be aware that to get the selected value you can simply call:
var elem = document.getElementById('selBoxOK');
var selName = elem.value;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Russ Suter
Russ Suter

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Russ Suter
Russ Suter

Just change .value to .innerText. That should give you the desired result.