Solved

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

Posted on 2003-12-05
4
181 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This article discusses four methods for overlaying images in a container on a web page
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
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…

758 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

17 Experts available now in Live!

Get 1:1 Help Now