Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jan 2003
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Getting "choice" value from a drop box.

    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>
    </select></td>

    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...

    THANK YOU!!

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!
    Last edited by whammy; 01-18-2003 at 01:22 AM.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #3
    Regular Coder
    Join Date
    Nov 2002
    Posts
    596
    Thanks
    0
    Thanked 0 Times in 0 Posts
    var sel = document.form_name.agent;
    var choice = sel[sel.selectedIndex].text;
    document.form_name.hidden_field_name.value = choice;

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    Code:
    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!
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #5
    Regular Coder
    Join Date
    Nov 2002
    Posts
    596
    Thanks
    0
    Thanked 0 Times in 0 Posts
    in that case shouldn't it be:.....options[....
    No. Unless you like typing.

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?
    Last edited by whammy; 01-18-2003 at 01:50 AM.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #7
    New to the CF scene
    Join Date
    Jan 2003
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •