Search code examples
javascriptinternet-explorerselectedindex

selectedIndex undefined in IE


I saw a few posts similar to my problem and tried the solutions offered, but I'm still having issues with IE8 & IE9 and 'selectedIndex'. This code returns my variable answerSubmitted as 'undefined':

var answerSubmitted = document.getElementById("DropDown-Answers").selectedIndex;

The above works perfectly in all other browsers. Based on another post here, I also tried this:

var answerSubmitted = document.getElementById("DropDown-Answers").value;

Still the same results - works elsewhere, but not in IE8 or IE9. I've verified that IE is recognizing that particular element by its ID.

Any guidance here?

MORE INFO: I'm creating the drop down menu dynamically by going thru a loop and adding variable text between the option and /option tags, like so (note that 'tempRandom' is a random number updated each time thru the loop):

tempMenuText = tempMenuText + "<option>" + Answers[tempRandom] + "</option>";     

The results are surrounded by the form an select tags, then I update the innerHTML of my element. This works and generates a working drop down menu. But... perhaps this is a clue: when I put a test with the innerHTML of the menu element into another element to view it, it shows as empty. It's as though IE is not seeing that there is HTML in the element, thinks it is null, and therefore 'selectedIndex' fails as null.


Solution

  • This is solved. It turns out it was my error in how I was referring to the ID of the selected item. An associate explained that .selectedIndex only works (in IE, at least), when the ID within the 'select' tag is correct. If not, it returns null, which is what was originally happening. All is good now. Thanks for the suggestions.