Wrox Programmer Forums
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 9th, 2005, 02:01 PM
Registered User
 
Join Date: Sep 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Get dropdown Value & Label

I Have a drop down listbox, Once the form has been submited I need to get the VALUE & LABEL.

I used request.form("listboxname") but this only gives me the value selected

[u]How do I get both the Value & Label from the drop down list?</u>
Hopefully it can be done with javascript/ASP or VB script.

thanks for any help?

 
Old September 12th, 2005, 11:39 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You can't do it using pure HTML but one way using JavaScript would be to set a hidden field with the value:
Code:
<form name="myForm" onsubmit="addOptionText();">
<select name="lstUsers" id="lstUsers">
  <option value="1">Joe</option>
  <option value="2">Pete</option>
  <option value="3">Steve</option>
</select>
<input type="hidden" name="lstUserText" id="txtUserText">
</form>
in your script block:
Code:
function addOptionText()
{
  var lst = document.getElementById("lstUsers");
  var txtHidden = document.getElementById("txtUserText");
  txtHidden.value = lst.options[lst.selectedIndex].text;
  return true;
}
Now your server will pick up the text part in lstUserText and the value part in lstUsers.

Change the hidden element to a text one if you want to debug.

--

Joe (Microsoft MVP - XML)
 
Old September 12th, 2005, 03:45 PM
Registered User
 
Join Date: Sep 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks!

The javascript does not seem to be working, when I select an item from the list it does not show up in the text box.
I did changed hidden to text. thanks again

<form name="myForm" onsubmit="addOptionText();">
<select name="lstUsers" id="lstUsers">
  <option value="1">Joe</option>
  <option value="2">Pete</option>
  <option value="3">Steve</option>
</select>
<input type="text" name="lstUserText" id="txtUserText">
</form>

<script language="JavaScript" type="text/JavaScript">

function addOptionText()
{
  var lst = document.getElementById("lstUsers");
  var txtHidden = document.getElementById("txtUserText");
  txtHidden.value = lst.options[lst.selectedIndex].text;
   return true;
}
</script>

 
Old September 13th, 2005, 03:25 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

It will only change when you submit the form. If you want it to change when you choose a selection then remove the form's onsubmit handler and add onchange="addOptionText();" to the select element.

--

Joe (Microsoft MVP - XML)
 
Old September 13th, 2005, 08:14 AM
Registered User
 
Join Date: Sep 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok, Thank you!:)






Similar Threads
Thread Thread Starter Forum Replies Last Post
how to see a label hombre Java Basics 3 March 4th, 2008 06:15 AM
Javascript && keeps turnig into &amp;&amp; ayrton Pro VB.NET 2002/2003 3 June 27th, 2005 03:34 PM
Linux & KDE & C++ & QT & MYSQL & Kdevelop Munnnki Linux 0 January 2nd, 2005 05:41 PM
Extract text from text file & put in dropdown box tsukey Beginning PHP 5 July 20th, 2004 09:49 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.