By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,318 Members | 2,340 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,318 IT Pros & Developers. It's quick & easy.

<OPTION> Tag and Values

P: n/a
How do I retrive the Name of the item in a select box

I have the following form
<select name="select">
<option value="1">Monday</option>
<option value="2">Tuesday</option>
<option value="3">Wednesday</option>
<option value="4">Thursday</option>
</select>

I have a button on the page that i want to alert a message when an item in
the select box has been chosen

eg If Monday was chosen (value 1), the alert should read You selected Monday
Many Thanks

Craig
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Lee
Craig Keightley said:

How do I retrive the Name of the item in a select box

I have the following form
<select name="select">
<option value="1">Monday</option>
<option value="2">Tuesday</option>
<option value="3">Wednesday</option>
<option value="4">Thursday</option>
</select>

I have a button on the page that i want to alert a message when an item in
the select box has been chosen

eg If Monday was chosen (value 1), the alert should read You selected Monday


What you're calling the "Name" is the "text" attribute of the
selected option.

So you would use the method describe in the FAQ, but substitute
"text" for "value":
http://www.jibbering.com/faq/#FAQ4_13

Jul 23 '05 #2

P: n/a
Craig Keightley wrote:
How do I retrive the Name of the item in a select box

I have the following form
<select name="select">
<option value="1">Monday</option>
<option value="2">Tuesday</option>
<option value="3">Wednesday</option>
<option value="4">Thursday</option>
</select>

I have a button on the page that i want to alert a message when an item in
the select box has been chosen

eg If Monday was chosen (value 1), the alert should read You selected Monday


<script type="text/javascript">
function showText(){
el=document.forms['myForm'].elements['mySelect'];

//get the current selections index
elIndex = el.selectedIndex;

//get the current selections text
elText = el.options[elIndex].text;

alert('Since you, as the user, are too stupid, blind or ignorant ' +
'to look at the select list you just changed, I find it, as the ' +
'author, to give you an annoying alert box to let you know you chose ' +
elText)
}
</script>
<form name="myForm">
<select name="mySelect">
<option value="1">Monday</option>
<option value="2">Tuesday</option>
<option value="3">Wednesday</option>
<option value="4">Thursday</option>
</select>
<input type="button" onclick="showText()" value="Show Me">

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.