Solved

Obtaining Label or Name from a FORM--not just value.

Posted on 2003-12-05
4
184 Views
Last Modified: 2010-04-09
A drop down menu list:

<option value="82347982374">DrugA</option>
<option value="82323432374">DrugB</option>
<option value="823wererrt34">DrugC</option>
<option value="82asf82374">DrugD</option>
<option value="8234sfsfft74">DrugE</option>
<option value="8234235d74">DrugF</option>

After choose this drop down, it'll submit to an action page and I like to say: You chose, among other things being displayed, Drug F.

How is this accomplish?
0
Comment
Question by:Danniken
4 Comments
 
LVL 11

Accepted Solution

by:
Zontar earned 25 total points
ID: 9886573
Only the value of the option is passed, not the text. Exception: if there's no value attribute* for an option, then its text is passed instead.

If you want something else displayed other than the value, you'll have to code it using an array/hash/other data structure or retrieve it from a database.

Another solution is to use JavaScript to get the text of the selected option and store it in a hidden form field prior to submission:

formObj.hiddenField.value = formObj.selectName.options[formObj.selectName.selectedIndex].text;

---------------------------------------

*That is, if the value attribute's not present at all; value="" will pass an empty string.
0
 
LVL 16

Assisted Solution

by:jaysolomon
jaysolomon earned 25 total points
ID: 9887037
here is an example of getting the selectedIndex text and value at the same time.

I made it alert on purpose, so you could see. You would want to change the doIt(selObj) function so that it will pass the value and or the text to the next page. Or if its a pop up window you can use document.write("You Chose: "+ getSelectText(opener.document.forms[0].sel1 +"   The value is "+ getSelectValue(opener.document.forms[0].sel1));

code

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
<!--Begin
function getSelectText(selObj)
{
      return selObj.options[selObj.selectedIndex].text;
}
function getSelectValue(selObj)
{
      return selObj.options[selObj.selectedIndex].value;
}
function doIt(selObj)
{
      alert("You Chose: "+ getSelectText(selObj) +"\n\nValue = "+ getSelectValue(selObj));
}
//End-->
</script>
</head>
<body>
<form name="form1" method="post" action="">
<select name="sel1" onchange="doIt(this);">
<option>Select-One</option>
<option value="82347982374">DrugA</option>
<option value="82323432374">DrugB</option>
<option value="823wererrt34">DrugC</option>
<option value="82asf82374">DrugD</option>
<option value="8234sfsfft74">DrugE</option>
<option value="8234235d74">DrugF</option>
</select>
</form>
</body>
</html>
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now