www.webdeveloper.com
Results 1 to 3 of 3

Thread: Show/Hide text field when Other is Selected

  1. #1
    Join Date
    Dec 2008
    Posts
    131

    Show/Hide text field when Other is Selected

    Hi I have found the below script that works for me to show/hide a text field when an option is selected. However, I want to make sure that the value entered in the text field is capture and post when the form is submitted. How do I do that? Thanks.


    HTML Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    
    <head>
      <title></title>
    <script language="JavaScript" type="text/javascript">
    function ShowTB(obj,id){
     txt=obj.options[obj.selectedIndex].text;
     if (txt.match('other')){
      document.getElementById(id).style.visibility='visible';
     }
     else{
      document.getElementById(id).style.visibility='hidden';
     }
    }
    </script>
    </head>
    
    <body>
    <select onchange="ShowTB(this,'fred');" >
    <option >tom</option>
    <option >dick</option>
    <option >smith</option>
    <option >other</option>
    </select>
    <br>
    <input id="fred" style="visibility:hidden;" >
    </body>
    
    </html>

  2. #2
    Join Date
    Dec 2008
    Posts
    131
    I also wants to know how do I validate the text field needs to be fill up if other is selected. thx.

  3. #3
    Join Date
    Feb 2013
    Location
    New York
    Posts
    6
    Wrap the select in a form. Give your input a name and a submit button.

    index.html file
    HTML Code:
    <form method="POST" action="second.php">
        <select onchange="ShowTB(this,'fred');" >
            <option >tom</option>
            <option >dick</option>
            <option >smith</option>
            <option >other</option>
        </select>
        <br>
        <input id="fred" type="text" name="fred" style="visibility:hidden;" />
        <br />
        <input id="save" type="submit"  value="Save" />
    </form>

    second.php file
    HTML Code:
    <?php echo $_POST['fred']; ?>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles