Need help changing value in textbox?

Discussion in 'JavaScript' started by tobydawson13, Jul 29, 2010.

  1. #1
    Hi, I currently have this code below which when I change the value in the dropdown list (id=changer) the value in the textbox also changes, try it out you'll see what I mean. As you can see "TEXTHERE" appears each time. Now say I need to use this script more than once on the same page, but with a different word instead of "TEXTHERE" each time. How would I go about doing this without having to write a seperate script each time? Sorry if you don't understand, I found it quite hard to explain. Would really appreciate any help, Thank you :)

    <script type="text/javascript">
    function change( txtBox )
    {
    var changer = document.getElementById('changer');
    if(changer.value != "1"){document.getElementById('ref').value = 'TEXTHERE{$encode}';}else{document.getElementById('ref').value = 'TEXTHERE{$signin_username}';}
    }
    </script>
    
    <select id='changer' onclick="change(this);" >
    <option value="1">{$signin_username}</option>
    <option value="2">{$encode}</option>
    </select>
    
    <input type="text" value="TEXTHERE{$signin_username}" readonly="readonly" id="ref" onclick="document.getElementById('ref').select();" />
    
    HTML:

     
    tobydawson13, Jul 29, 2010 IP
  2. ThomasTwen

    ThomasTwen Peon

    Messages:
    113
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <script type="text/javascript">
    function change( txtBox, resp_str )
    {
    var changer = document.getElementById('changer');
    if(changer.value != "1"){document.getElementById('ref').value = resp_str + '{$encode}';}else{document.getElementById('ref').value = resp_str + '{$signin_username}';}
    }
    </script>
    
    <select id='changer' onclick="change(this, 'lol wut');" >
    <option value="1">{$signin_username}</option>
    <option value="2">{$encode}</option>
    </select>
    
    <input type="text" value="TEXTHERE{$signin_username}" readonly="readonly" id="ref" onclick="document.getElementById('ref').select();" />
    HTML:

    this script will do the same thing, except that it will show "lol wut" instead of "TEXTHERE".
    the line
    onclick="change(this, 'lol wut');"
    Code (markup):
    is the one where you can set the string to a different value
     
    ThomasTwen, Jul 29, 2010 IP
  3. hauntin

    hauntin Peon

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    exactly said. This(not the this in the code) will change the current content to your desired text.
     
    hauntin, Jul 30, 2010 IP