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 4 of 4
  1. #1
    New Coder
    Join Date
    Feb 2006
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to take a value from a Select Box

    ok so a select box (<select name="blabla"><option>bla</option</select) how do i trap the option selected (onClick event) and place the value selected into a text box? I am a PHP programmer and the page refresh issue gets in the way of what i need to accomplish, javascript can do it!

    Cheers

  • #2
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    You can use the onchange event handler, try this:
    PHP Code:
    <html>
    <
    head>
    <
    script type="text/javascript">
    function 
    move(seltxt)
    {
      
    txt.value sel.options[sel.selectedIndex].value;
    }
    </script>
    </head>
    <body>
    <form>
    <select name="test" onchange="move(this, txt);">
      <option value="boo1">boo1</option>
      <option value="boo2">boo2</option>
      <option value="boo3">boo3</option>
      <option value="boo4">boo4</option>
    </select>
    <br>
    <input type="text" name="txt">
    </form>
    </body>
    </html> 
    Good luck;

  • #3
    New Coder
    Join Date
    Feb 2006
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks

    One question though, the function "move()" in the onchange="move(this, txt)" what is "this"?? its not defined anywhere

  • #4
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    'this' refers to the select element. It would be the same as passing it's name to the function.
    Heres a good reference:
    http://www.quirksmode.org/js/this.html

    Good luck;


  •  

    Posting Permissions

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