SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,618
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    getting the text in a select box

    I have a select box with a hidden box underneath,
    HTML Code:
    <select name="provider_id" id="Username" class="select"> <option value=4>anyone</option>
    <option value=2>Balena</option>
    <option value=3>Eschimossa</option>
    <option value=14>matts girl</option>
    <option value=1>Sandra J.</option>                    
    </select>
    what im trying to do is capture is the other value (anyone, etc...) depending on which option is chosen in a hidden input box.
    HTML Code:
     <input type="hidden" value="" name="provider_name">
    I gather what need to do is use the onChange thing on the select box to capture the current texts value and somehow put it in my hidden thing.

    How can I do this?
    Thx...
    "Oh, and Jenkins--apparently your mother died this morning."

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,890
    Mentioned
    211 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    So, just to be clear, you want to dynamically set the value of the hidden input element to the text of whatever option is selected in the select element (e.g. anyone, Balena, Eschimossa).
    Is that correct?
    Are you already using jQuery in your page?

  3. #3
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,618
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Yes, thats exactly what im trying to accomplish, Im only using HTML form elements and nothing-else. /Should I use jquery?
    "Oh, and Jenkins--apparently your mother died this morning."

  4. #4
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,890
    Mentioned
    211 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by lukeurtnowski View Post
    Should I use jquery?
    No. It makes the job 5% easier, so I would have used it if you had been using it.

    This should do the trick:

    HTML Code:
    <!DOCTYPE HTML>
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Dynamically set value of hidden input</title>
      </head>
    
      <body>
        <form>
          <select name="provider_id" id="Username" class="select">
            <option value=4>anyone</option>
            <option value=2>Balena</option>
            <option value=3>Eschimossa</option>
            <option value=14>matts girl</option>
            <option value=1>Sandra J.</option>                    
          </select>
          
          <input type="hidden" value="anyone" name="provider_name" id="hidden">
        </form>
        
        <script>
          var sel = document.getElementById("Username"),
                    hidden = document.getElementById("hidden");
    				
          sel.onchange=function(){
            hidden.value = sel.options[sel.selectedIndex].text;
          }
        </script>
      </body>
    </html>

  5. #5
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,618
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    thx
    "Oh, and Jenkins--apparently your mother died this morning."


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
  •