Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 9th, 2005, 02:01 PM
Registered User
 
Join Date: Sep 2005
Location: , , .
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?

Reply With Quote
  #2 (permalink)  
Old September 12th, 2005, 11:39 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
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)
Reply With Quote
  #3 (permalink)  
Old September 12th, 2005, 03:45 PM
Registered User
 
Join Date: Sep 2005
Location: , , .
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>

Reply With Quote
  #4 (permalink)  
Old September 13th, 2005, 03:25 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
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)
Reply With Quote
  #5 (permalink)  
Old September 13th, 2005, 08:14 AM
Registered User
 
Join Date: Sep 2005
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok, Thank you!:)

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 01:22 PM.


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