Click to See Complete Forum and Search --> : Disable form elements dynamically


RavenWind
01-05-2004, 09:47 PM
I am currently working on a project that requires the disabling of form elements based on other form elements. For example, you have a question and two radio buttons that answer it "yes" and "no". If the user says yes, a text box is enabled, if no is selected, the text box becomes disabled. I would like to know how to dynamically alter the disabled attribute. Much thanks in advance,

Kenneth

fredmv
01-05-2004, 09:59 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>untitled</title>
<meta http-equiv="content-type" content="application/xhtml+xml; charset=iso-8859-1" />
<script type="text/javascript">
//<![CDATA[
function toggle(e)
{
e.disabled = !e.disabled;
}
//]]>
</script>
</head>
<body>

<form action="#">
<div>
<input type="text" disabled="disabled" />
<label for="r1">
Yes
</label>
<input type="radio" onclick="toggle(elements[0]);" id="r1" name="d" />
<label for="r2">
No
</label>
<input type="radio" onclick="toggle(elements[0]);" id="r2" name="d" />
</div>
</form>
</body>
</html>

RavenWind
01-05-2004, 10:20 PM
thanks! one last question. if the element being disabled was a set of radio buttons, how would one do the js to disable/reenable both? i have two yes/no questions, if they answer no to the first, the second is irrelivant, so i want it disabled. your method works well, but only affects a single element. hope you understand what i mean, and can help. Thanks again,

Kenneth