SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Feb 2004
    Location
    Winnipeg Canada
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Retrieve Select BOX "Label" rather than "Value"

    Is it possible to retrieve the "label" of a select box rather than the "value" with JavaScript?

    Example:
    1: I have a select box named "dogs" and resides in a form titled "form1".
    2: There are 3 entries in the select box.
    <option value="1">Poodle</option>
    <option value="2">Terrier</option>
    <option value="3">Hound</option>

    The VALUE is the database_ID while the label is the type of dog. Although I wan't PHP to handle the value accordingly (database manipulation), I also want to use Javascript to grab the "friendly label name" so I can manuipulate it as desired (create new variables, display big text, drop the friendly name in a new text box or hidden fields, etc... etc... etc...) If I can grab it, I can use it

    "document.form1.cats.value" will grab or display the choice of the value, but I want to grab the label name!

    Any help would be appreciated! Thanks.

  2. #2
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How are you doing your labels?

    Are you wrapping them around the input elements, or do you have them preceding the input elements?

    Can we see all the code please? (or a link )
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  3. #3
    SitePoint Zealot
    Join Date
    Feb 2004
    Location
    Winnipeg Canada
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Label might not have been the correct choice of words. Here is a link to what I am trying to accomplish in it's simplest of ways. View Source should reveal all of the code.

    http://www.wpgumbo.com/dogs.htm

    All I really want to know is, how can I drop the name of the dog into the text box, rather than the VALUE (1,2,3,4).

    In the real world, the value will be a "Database ID#" which is why I simulated that here. The friendly name is what I want to capture straight from the select box if possible.

    Hope that makes sense.

  4. #4
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This should do it:

    Code Javascript:
    function movedog()
    {
    document.form1.chosendog.value = document.form1.dogs.getElementsByTagName('option')[document.form1.dogs.selectedIndex].firstChild.nodeValue;
    }
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  5. #5
    SitePoint Zealot
    Join Date
    Feb 2004
    Location
    Winnipeg Canada
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yip - that took the dog for a walk! Exactly what I wanted.
    Thanks for pointing me the right direction, it's appreciated.

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    You don't need to use .firstChild.nodeValue

    You can use .text instead to get the text of an option.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Isn't .text a non-standard property though? Even Firebug doesn't show it as being available
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

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