Friday, July 16, 2010

Javascript - retrieving the drop down selected value

I copied this from the Tek-Tips forum just so I have a repository of it.

<head>
<title>New Page 10</title>
<script Language="JavaScript">
function checkData()
{

var myTest = me.D1.selectedIndex.value;
alert(myTest);
}
</script>

</head>

<body>

<form method="POST" name="me">
<select size="1" name="D1" onChange="checkData()">
<option value="99">Default</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>

</body>

</html>

selectedIndex, when used correctly, returns the index (number) of the space in the options collection in which the selected option resides.

Here's what you want to use:

me.D1.options[me.D1.selectedIndex].value

I think that's it. Try the following if not:

me.D1.options[me.D1.options.selectedIndex].value

No comments:

Post a Comment