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;
         }
JavaScriptHTMLWeb Development

Avatar of undefined
Last Comment
Russ Suter

8/22/2022 - Mon
Alexandre Simões

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
Russ Suter

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Russ Suter

Just change .value to .innerText. That should give you the desired result.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes