Forum Moderators: open

Message Too Old, No Replies

getting the value of a select tag

         

tores

11:17 am on Dec 31, 2004 (gmt 0)

10+ Year Member



Hi

I have this select-tag with some options. What I want is to store the value displayed in the dropdown menu ,defined by the select, in a javascript variable. How would I do this?

regards tores

tores

1:49 pm on Dec 31, 2004 (gmt 0)

10+ Year Member



Solution:

<html>
<head>
<script type="text/javascript">
function getText()
{
var x=document.getElementById("mySelect")
alert(x.options[x.selectedIndex].text)
}
</script>
</head>

<body>
<form>
Select your favorite fruit:
<select id="mySelect">
<option>Apple</option>
<option>Orange</option>
<option>Pineapple</option>
<option>Banana</option>
</select>
<br /><br />
<input type="button" onclick="getText()" value="Show text of selected fruit">
</form>
</body>