0

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>
4
  • What does the actual rendered HTML look like? That would be much more useful seeing the script will also interact with that. It would show the actual rendered properties and so on. Commented Sep 12, 2012 at 21:59
  • 1
    I'm hoping "everything you've read online" didn't include the .value after the getElementById() call... :) Commented Sep 12, 2012 at 22:00
  • Where did you get the id AdvOrBasicSearch_advSearch_ddlState from? Commented Sep 12, 2012 at 22:02
  • 2
    I can barely read asp but this doesn't seem right: temp = document.getElementById("AdvOrBasicSearch_advSearch_ddlState").value. If I had to take an educated guess, I'd drop the .value Commented Sep 12, 2012 at 22:02

1 Answer 1

5

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;
Sign up to request clarification or add additional context in comments.

1 Comment

I just came to the same in this fiddle: fiddle.jshell.net/RrNyV As a general side-note: If you have issues like that you can use console.log(temp) for example after populating the variable and check the console outputs in the browser debugging tools. Can save you a lot of headaches.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.