Grab selected text from the <SELECT>

Hi,

I am trying to get the value of the selected text from <SELECT> with the follow code

var gnm = document.getElementById(“nm”);
var sval = ngnm.options[ngnm.selectedIndex].text;
alert(sval);

but i am getting an error as follows:

Error Message:
Line: 129
char: 2
Error: Object expected
Code: 0

The line 129 belongs to alert(sval);. I do not understand why it is displaying an error for alert?
Please let me how to grab the selected text from the dropdown menu.
Thank you for your help in advance.

You need to post some HTML. Who is “nm”? A form?

var sval = ngnm.options[ngnm.selectedIndex].text;

Who is ngnm? You have not introduced him.

An example of working:


HTML:
<select id="someSelect">
...
JS:
var theSelect = document.getElementById('someSelect');
var selectedOption = theSelect.selectedIndex;
var selectedOptionText = theSelect.options[selectedOption].text;

Did you accidently put an “n” infront of the gnm variable?

Try


var sval = gnm.options[ngnm.selectedIndex].text;

Did you accidently put an “n” infront of the gnm variable?

I wondered the same thing.

Thanks you Stomme poes and bals28mjk i was not using the correct id to call the selectedIndex