?
Solved

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

Posted on 2003-12-05
4
Medium Priority
?
189 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 11

Accepted Solution

by:
Zontar earned 100 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 100 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

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

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

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
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 …
Suggested Courses

765 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