onchange problem?

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

  1. #1
    Hi there, I've got this code, the value of the dropdown list is "{$signin_username}", and the text in bold (id=change) is also "{$signin_username}", when I change the value in the dropdown list to "{$encode}" the text in bold also changes to "{$encode}", but then when I change the value back to the original "{$signin_username}" it does not work and the text in bold doesn't change. How can I get this to work?

    Thanks

    <script type="text/javascript">
    function alertOnChange( txtBox )
    {
    document.getElementById('change').innerHTML = '{$encode}';
    }
    </script>
    
    <select onchange="alertOnChange( this );" >
    <option value="1">{$signin_username}</option>
    <option value="2">{$encode}</option>
    </select>
    
    <b id='change'>{$signin_username}</b>
    HTML:
     
    tobydawson13, Jul 15, 2010 IP
  2. tobydawson13

    tobydawson13 Active Member

    Messages:
    645
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #2
    I managed to fix it with this code.

    <script type="text/javascript">
    function alertOnChange( txtBox )
    {
    var myTextField = document.getElementById('changer');
    
    if(myTextField.value != "1"){
    document.getElementById('change').innerHTML = '{$encode}';
    }
    
    if(myTextField.value == "1")
    {
    document.getElementById('change').innerHTML = '{$signin_username}';
    }
    
    }
    </script>
    
    <select id='changer' onclick="alertOnChange( this );" >
    <option value="1">{$signin_username}</option>
    <option value="2">{$encode}</option>
    </select>
    
    <b id='change'>{$signin_username}</b>
    HTML:
     
    tobydawson13, Jul 15, 2010 IP
  3. yohanip

    yohanip Well-Known Member

    Messages:
    350
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    108
    #3
    the onchange variable is not reliable, especially on ie, glad you manage it to the "click" event.. :D it's more dependable
     
    yohanip, Jul 21, 2010 IP