How to make a box show up only if a option is selected

Discussion in 'PHP' started by HellBomb, Aug 13, 2010.

  1. #1
    Ok, I am working on a sleek Contact Us page and I want to be able to have the "other" option be selectable and people can enter their own subject. Well to do this I want to make it so if you select the "other" option in the drop down menu in a text box will show up below it. So far I got everything else done on the drop down menu except that.
     
    HellBomb, Aug 13, 2010 IP
  2. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #2
    A simple example how you can do it:

    
    <html>
    
    <head>
      <title>Example</title>
    </head>
    
    <script type="text/javascript">
    function changed(){
      if(document.getElementById("dropdown").value=="other"){
        the_textbox = document.createElement("textarea");
        the_textbox.id = "own_subject";
        the_textbox.name = "own_subject";
        document.getElementById("textbox").appendChild(the_textbox);
      }else{
        if(document.getElementById("own_subject")){
            document.getElementById("textbox").removeChild(document.getElementById("own_subject"));
        }
      }
    }
    </script>
    
    <body>
    
    <select id="dropdown" onchange="changed();">
        <option value="this">This</option>
        <option value="other">Other</option>
    </select>
    <div id="textbox"></div>
    
    </body>
    
    </html>
    
    Code (JavaScript):
     
    s_ruben, Aug 13, 2010 IP
  3. HellBomb

    HellBomb Active Member

    Messages:
    125
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    80
    #3
    Wow thank you so much. This will really liven up my contact us page. I seriously need to learn javascript myself.
     
    HellBomb, Aug 14, 2010 IP