Jump to content

Radio button to enable text box


New Coder

Recommended Posts

Hello All,

On my form I have 8 radio buttons one of which the value is "other" I have a text box next to "other" so that we can capture what the other might be. (Standard stuff I'm sure you've seen)

I have got it so that the text box is enabled if they select the "other" radio button, but it's not being disabled again if they decide to pick a different option and my limited javascript knowledge,I'm stuck.

Heres what I have so far:

<script type="text/javascript">
var currentEnabled = null;
function enableElement(elem)
{
if (currentEnabled)
{
	currentEnabled.disabled = true;
}
elem.disabled = false;
currentEnabled = elem;
}
</script>

 

list .= "<tr>";
$list .= "<td class=table width=\"50%\"><input type=\"radio\" class=\"radio_button\" name=\"destination\" value=\"1\">Spain</td>";	
$list .= "<td class=table width=\"50%\"><input type=\"radio\" class=\"radio_button\" name=\"destination\" value=\"2\">France</td>";
$list .= "</tr>";
$list .= "<tr>";
$list .= "<td class=table width=\"50%\"><input type=\"radio\" class=\"radio_button\" name=\"destination\" onclick=\"enableElement(this.form.elements['other_desc'])\" value=\"99\">Other</td>";	
$list .= "<td class=table width=\"50%\"><input type=\"text\" class=\"textdesc\" style=\"color:lightgrey\" size=\"50\" name=\"other_desc\" value=\"If other enter detail here\" disabled=\"disabled\" onfocus=\"if (this.className=='textdesc') { this.className = ''; this.value = '';this.style.color = 'black';}\"  onblur=\"if (this.value == '') { this.className = 'textdesc'; this.value = 'If other enter detail here'; this.style.color = 'lightgrey'; }\" ></td>";
$list .= "</tr>";

 

Above is only a snipit :-)

 

Any help much appreciated.

 

Many Thanks

Link to comment
Share on other sites

Hi,

 

You would need to put the on click function call on every radio button.

 

So..

 


<input type=\"radio\" class=\"radio_button\" name=\"destination\" value=\"1\" onclick=\"enableElement();\">Spain
<input type=\"radio\" class=\"radio_button\" name=\"destination\" value=\"2\" onclick=\"enableElement();\">France
<input type=\"radio\" class=\"radio_button\" name=\"destination\" value=\"3\" onclick=\"enableElement();\">Other

function enableElement ()
{
var radioValue = '';
var selection = document.getElementsByName('destination');

for (i=0; i<selection.length; i++)
{
	if (selection[i].checked)
	{
		radioValue = selection[i].value;
		break;
	}
}

if (radioValue == 'other')
{
	//enable text box
}
else
{
	//disable text box
}
}

 

I havent tested this code but hopefully it will help you on your way.

 

jug

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.