[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2003-12-05
4
Medium Priority
?
192 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
2 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Suggested Courses

834 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