View Full Version : Selected option question
Lazaroth
08-16-2003, 04:10 PM
When you click on an item below how do you alert() the name?
I.e. I click the first and "News" popup.
<select name="the_list">
<option value="news.htm">News
<option value="new.htm">Whats new
<option value="r_new.htm">Really new things
</select>
ACJavascript
08-16-2003, 05:52 PM
You could do this
<script language="javascript">
function goGetit(){
alert("Name:" +document.TheForm.the_list.value)
}
<form name="TheForm">
<select name="the_list" onChange="goGetit()">
<option value="news.htm">News
<option value="new.htm">Whats new
<option value="r_new.htm">Really new things
</select>
</form>
<script language="javascript">
function goGetit(n){
alert(n)
}
</script>
<form name="TheForm">
<select name="the_list" onChange="goGetit(TheForm.the_list.options[TheForm.the_list.options.selectedIndex].text)">
<option value=""></option>
<option value="news.htm">News</option>
<option value="new.htm">Whats new</option>
<option value="r_new.htm">Really new things</option>
</select>
</form>
cheesebag
08-16-2003, 11:07 PM
...unless you enjoy typing:
<html>
<head>
</head>
<body>
<form>
<select name="the_list" onchange="alert(options[selectedIndex].text)">
<option value="news.htm">News</option>
<option value="new.htm">Whats new</option>
<option value="r_new.htm">Really new things</option>
</form>
</select>
</body>
</html>
Always use the closing </option> tag.
http://www.devguru.com/Technologies/ecmascript/quickref/option.html
Vincent Puglia
08-17-2003, 01:35 AM
Hi Lazaroth,
In cheesebag's code, the following:
</form>
</select>
should be reversed, though it does seem to work in IE as is
...unless you enjoy typing:
onchange="alert(this[selectedIndex].text)">
:)
Vinny
Typings not bad ........ honest .... it isn't
:D :D :D
Lazaroth
08-17-2003, 09:01 PM
Thanks! :)
cheesebag
08-17-2003, 09:59 PM
Oops! :o
Thanks Vinny. Everything works in IE 'as is'.
J: Typing: bad. Prototyping: good. :thumbsup:
Vincent Puglia
08-18-2003, 06:26 AM
Hi,
Everything works in IE 'as is'.
As opposed to those things that do nothing? :)
Minimal code needed:
<select name="the_list" onchange="alert(options[selectedIndex].text)">
<option value="news.htm">News
<option value="new.htm">Whats new
<option value="r_new.htm">Really new things
Note: no html, head, body, and form tags; no closing tags
Vinny
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.