NealR
NealR

Reputation: 10689

Getting selected index from drop down list in javascript

Everything I've read online says this is the method and syntax I need to use to get the selected index from a drop down list.

var temp;
temp = document.getElementById("AdvOrBasicSearch_advSearch_ddlState").value;
var sState = temp.options[temp.selectedIndex].text;

However, I get the following error on the last line:

"Microsoft JScript runtime error: 'options' is null or not an object"

Below is a sampling of the drop down list (no need to post all 50 states)

<td><asp:dropdownlist id="ddlState" tabIndex="8" runat="server" EnableViewState="False" Width="150px"
    CssClass="clsTextInput">
    <asp:ListItem Value=""></asp:ListItem>
    <asp:ListItem Value="AL">Alabama</asp:ListItem>
    <asp:ListItem Value="AK">Alaska</asp:ListItem>
</asp:dropdownlist></td>

Upvotes: 0

Views: 11016

Answers (1)

NealR
NealR

Reputation: 10689

Thanks to the comments I dropped the .value and everything works fine. Thx guys!

var temp;
temp = document.getElementById("AdvOrBasicSearch_advSearch_ddlState");
sState = temp.options[temp.selectedIndex].text;

Upvotes: 5

Related Questions