View Full Version : Make a text field mandatory if radio button selected

05-05-2009, 06:44 PM
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.


05-06-2009, 01:41 AM
I suggest having a fallback, such as if the user doesn't have javascript (the below script should do just that).

<script type="text/javascript">
var textbox = function(me){
if(me.checked == false){
var textb = document.createElement('input');
textb.type = "text";
textb.name = "text";
if(me.checked == false){
return false;
}, 50);
<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>