View Full Version : Getting "choice" value from a drop box.

01-18-2003, 01:59 AM
The following is the code from a drop-box on my web page.

<select size="1" name="agent" style="background-color: #BDBDD2">
<option value="def">Select an Agent</option>
<option value="0">Mary Church</option>
<option value="1">John Litster</option>
<option value="2">Linda Miller</option>
<option value="3">Andre Goudreau</option>

Now, I know how to easily get the value, but I would like to know how to get the "name" or "choice" (In this example, it would be Mary Church, John Litster, Linda Miller, or Andre Goudreau). I want to get the choice, after the user has made his selection from the drop-box.

I would then like to transfer that choice to a hidden field or textbox...


01-18-2003, 02:13 AM
If you need that info (i.e. 0,1,2,3), then I would store it in an associated array, or parse it server-side, etc..

Otherwise, what are you using the "values" for?

Let me ask that another way... if you aren't using 0,1,2,3 instead of Mary Church, etc. then what are they for?

If you're using those values for some reason, then use them. Otherwise make the values whatever you want, but if you don't DEFINE the value, it should default to whatever you haven't defined, such as "Mary Church" etc. Example:

<option>Mary Church</option>
<option>John Litster</option>
<option>Linda Miller</option>
<option>Andre Goudreau</option>

However, I find it much easier (since I usually program database-driven websites) to define an option value, and then do something depending on the value that was selected.

Hope this helps! :)

01-18-2003, 02:34 AM
var sel = document.form_name.agent;
var choice = sel[sel.selectedIndex].text;
document.form_name.hidden_field_name.value = choice;

01-18-2003, 02:42 AM
You're right cheese, I keep forgetting about that since I never use client-side scripting to parse form input... I thought if there was no value specified that the value would default to the text selection... I know that's true in some cases...

Although, in this particular case shouldn't it be:

var sel = document.form_name.agent;
var choice = sel.options[sel.selectedIndex].text;
document.form_name.hidden_field_name.value = choice;


However, that still begs the question what he is trying to do with the values!

01-18-2003, 02:44 AM
in that case shouldn't it be:.....options[....
No. Unless you like typing. ;)

01-18-2003, 02:46 AM
You sure? Last time I checked it won't work in earlier versions of NetScrape otherwise. :) Like, NS 4.7x for instance. Or perhaps Mozilla? I don't know if Mozilla has adopted any of Microsoft's bad programming practices though... hopefully not, I think they require the full declaration, someone let me know if I'm off base here!

cheese, have you looked at this code in anything besides IE?

01-18-2003, 04:07 AM
Beautiful. Thank you for your help. The reason I needed the code was because I want to send both the number (0,1,2,3) and the corresponding name (Mary Church, John.. etc) to a CGI script on a server. And the name and corresponding number must be sent to the script in different parameters.

Thanks again!

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum