Results 1 to 2 of 2

Thread: Make a text field mandatory if radio button selected

  1. #1
    Join Date
    May 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Make a text field mandatory if radio button selected

    Hello all. I am a newbie here to Dynamic Drive and have very minimal knowledge of JavaScript. What I would like to do is just what the title says. I have a form with a radio button list and if the user select the "Other" option I want to make a text field next to the radio button required.

    Thank in advance for any help.

    mikeysan

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    I suggest having a fallback, such as if the user doesn't have javascript (the below script should do just that).
    Code:
    <script type="text/javascript">
    var textbox = function(me){
      if(me.checked == false){
        var textb = document.createElement('input');
        textb.type = "text";
        textb.name = "text";
        me.parentNode.appendChild(textb);
      }
      setInterval(function(){
        if(me.checked == false){
           me.parentNode.removeChild(textb);
           return false;
        }
      }, 50);
    };  
    </script>
    <form action="index.php" method="get">
    <input type="radio" name="radio" /><br />
    <input type="radio" name="radio" /><br />
    <input type="radio" name="radio" onmouseup="textbox(this)"/><noscript><input type="text" name="text" /> (Fill this in, if you've checked the radio next to this)</noscript>
    </form>
    Last edited by Nile; 05-06-2009 at 01:49 AM.
    Jeremy | jfein.net

Bookmarks

Posting Permissions

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