The below code doesn’t it print the value.
function go(x)
{
alert(x.options.selectedIndex.value);
//location=document.menu.student.options[document.menu.student.selectedIndex].value
}
this is the html code
<select name="student" onChange="go(this)">
<option selected> Student </option>
<option value="http://www.cnet.com">Attendence</option>
<option value="http://www.abc.com">Exams</option>
</select>
selectedIndexis a number, it doesn’t have avalueproperty.If you have a
selectelement which just allows single-select (as yours does), the easiest way to get its value is theselectelement’svalueproperty:Double-check that it works on the browsers you want to support, but MaryAnne (see the comments) has checked on all current major browsers and I’ve checked IE6, IE7, and Firefox 3.6 (e.g., older browsers), and they all work. Since it’s specified in DOM2 HTML (the link above)…
But re
selectedIndex, you probably meant:I’d probably take it a bit further and be more defensive:
…or
…in case there is no selected option (in which case,
optionwill beundefined), although with your particular markup and code, I’m not aware of a user agent where thechangeevent would be fired without there being anything selected. At least, I don’t think so — “I think” being the reason for being defensive. 🙂