Welcome to WebmasterWorld Guest from 54.81.69.220

Forum Moderators: open

Message Too Old, No Replies

getting the value of a select tag

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

New User

10+ Year Member

joined:Dec 29, 2004
posts:28
votes: 0


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

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

New User

10+ Year Member

joined:Dec 29, 2004
posts:28
votes: 0


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>