Please help wiith my code troubles :-) Form dropdown list into textbox.

Discussion in 'PHP' started by EatTheCookie, Jan 6, 2012.

  1. #1
    Hi what I am basically trying to do with this form is make a dropdown list output the selection into a textbox.
    I know it needs to be onchange but I can't seem to do this correctly with the options. Please help if you can.






    <select id="WHATOUTPUTS" name="state" onclick="document.getElementById('textbox').value = this.id">
    <option id="WHATDOESNOTOUTPUT" value="CA">California</option>
    <option value="WHATDOESNOTOUTPUT">Nevada</option>
    </select>


    <input type="text" id="textbox" value="" />
     
    EatTheCookie, Jan 6, 2012 IP
  2. startdream

    startdream Member

    Messages:
    76
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    28
    #2
    Hi, You can use event onchange for this action
    Same
     
    startdream, Jan 6, 2012 IP
  3. EatTheCookie

    EatTheCookie Greenhorn

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    Yes but the problem with this is it only changes on state.. it wont work for changing to other values (of lets say a list of 10 variables)




    <select id="WHATOUTPUTS" name="state" onchange="document.getElementById('textbox').value = this.id">
    <option id="WHATDOESNOTOUTPUT" value="CA">California</option>
    <option id="WHATDOESNOTOUTPUT2" value="CA">Nevada</option>
    <option id="WHATDOESNOTOUTPUT3" value="CA">UTAH</option>
    </select>
    <input type="text" id="textbox" value="" />
     
    EatTheCookie, Jan 6, 2012 IP
  4. JamesD31

    JamesD31 Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #4
    
    <select id="WHATOUTPUTS" name="state" onchange="document.getElementById('textbox').value = [LEFT][COLOR=#000000][FONT=Courier New]this.options[this.selectedIndex].value">
    
    Code (markup):
    That will grab the value for your selected option - i think to grab the "id" of it can be like this

    
    ... = [/FONT][/COLOR][COLOR=#000000][FONT=Courier New]this.options[this.selectedIndex].id">
    
    Code (markup):
    "this.id" is grabbing the actual element you are in (the selectbox) which is what "this" represents. You were looking for it's "options" and than select the appropriate one, as shown above.[/FONT][/COLOR][/LEFT]
     
    JamesD31, Jan 7, 2012 IP