PDA

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



mikeysan
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.

mikeysan

Nile
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";
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>