I need a piece of java that will hide OR show a text box if

Discussion in 'JavaScript' started by x0x, Aug 23, 2008.

  1. #1
    I need a piece of javascript that will hide OR show a text box IF a certain thing is selected from the dropdown.



    DROPDOWN:
    
    <select name="stuff" class="textField">
    
        <option value="" selected="selected" class="textField">-select one-</option>
       
      <option value="1" <? if($starts==1){echo"selected";}?>>one </option>
    
        <option value="2" <? if($starts==2){echo"selected";}?>>two</option>
    
    
      </select>
    HTML:
    When option "2" is selected a text box needs to appear next to the dropdown

    <input name="anotherthing" type="text" class="box" value="" /> 
    HTML:
    Can it be done?
     
    x0x, Aug 23, 2008 IP
  2. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #2
    I've done it!
    Found this
    <script type="text/javascript"> 
    function toggleField(val) { 
    var o = document.getElementById('other'); 
    (parseInt(val) == 99)? o.style.display = 'block' : o.style.display = 'none'; 
    } 
    </script> 
    
    and the form: 
    
    
    <form action="" method="post"> 
    <select name="sel" id="sel" onChange="toggleField(this.value);"> 
    <option value="0">Select</option> 
    <option value="1">One</option> 
    <option value="2">Two</option> 
    <option value="99">Other</option> 
    </select> 
    <input type="text" name="other" id="other" style="display: none;"> 
    </form>
    HTML:
     
    x0x, Aug 23, 2008 IP