Hacker
Hacker

Reputation: 7906

getting selected select box text

i am trying to use like

var modelLength     = document.getElementsByName("FK_BrandID")[0].text;

when i try to alert this it prints only undefined.. is this not proper..?

Upvotes: 0

Views: 6710

Answers (4)

Torres
Torres

Reputation: 5400

Everything you read above is correct, the problem in your code was, the options word was missing. with this your code should work:

var modelLength = document.getElementsByName("FK_BrandID")**.options**[0].text;

Upvotes: 0

Sarfraz
Sarfraz

Reputation: 382696

You can do like this:

var index = document.getElementById("FK_BrandID").selectedIndex;
var value = document.getElementById("FK_BrandID").options[index].text;
alert(value);

That will give you the text of selected option in the select box. To get the value of select box instead, use value

var value = document.getElementById("FK_BrandID").value;
alert(value);

Upvotes: 1

Nick Craver
Nick Craver

Reputation: 630389

I think this is what you're after:

var sel = document.getElementsByName("FK_BrandID")[0];
var modelLength = sel.options[sel.selectedIndex].text;

You can give it a try here

Upvotes: 1

Quentin
Quentin

Reputation: 943480

AFAIK, no element has a text property. Perhaps you are looking for value?

Upvotes: 0

Related Questions