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 to the CF scene
    Join Date
    Apr 2013
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Form and dropdown box question

    Here is a portion of my code. I would like for the item selected in the drop down box to appear in the text input box once selected. I'm not quite sure how to link the two

    Favorite Brand of Guitar: <input type="text" id="guitar"/>
    <select>
    <option value="Gibson">Gibson</option>
    <option value="Fender">Fender</option>
    <option value="Jackon">Jackson</option>
    <option value="Ibanez">Ibanez</option>
    <option value="ESP">ESP</option>
    <option value="Epiphone">Epiphone</option>
    <option value="PRS">PRS</option>
    <option value="G&L">G&L</option>
    <option value="Dean">Dean</option>
    <option value="Washburn">Washburn</option>
    <option value="BC Rich">BC Rich</option>
    </select>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Code:
    Favorite Brand of Guitar: <input type="text" id="guitar"/>
    <br>
    <select id = "mysel" onchange = "showtext()">
    <option value = "">Choose ...</option>
    <option value="Gibson">Gibson</option>
    <option value="Fender">Fender</option>
    <option value="Jackon">Jackson</option>
    <option value="Ibanez">Ibanez</option>
    <option value="ESP">ESP</option>
    <option value="Epiphone">Epiphone</option>
    <option value="PRS">PRS</option>
    <option value="G&L">G&L</option>
    <option value="Dean">Dean</option>
    <option value="Washburn">Washburn</option>
    <option value="BC Rich">BC Rich</option>
    </select> 
    
    <script type = "text/javascript">
    function showtext() {
    document.getElementById("guitar").value = document.getElementById("mysel").value;
    }
    </script>

    “Expert: a man who makes three correct guesses consecutively.”
    Dr. Laurence J. Peter (American "hierarchiologist", Educator and Writer, 1919-1990)

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    kse33 (04-18-2013)

  • #3
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Awesome. This worked perfectly!

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,966
    Thanks
    0
    Thanked 236 Times in 233 Posts
    You can simplify it by passing this (which refers to the current element which is the select) to the showtext() function.
    Code:
    <select id="mysel" onchange="showtext(this)">
    Code:
    function showtext(sel) {
        document.getElementById("guitar").value = sel.value;
    }
    Or you can do it directly without a function if you only ever need to set the textbox value and nothing else.
    Code:
    <select id="mysel" onchange="document.getElementById('guitar').value = this.value">


  •  

    Posting Permissions

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