/    Sign up×
Community /Pin to ProfileBookmark

Disable form elements dynamically

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

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@fredmvJan 06.2004 — <!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>

<i> </i> &lt;form action="#"&gt;
<i> </i> &lt;div&gt;
<i> </i> &lt;input type="text" disabled="disabled" /&gt;
<i> </i> &lt;label for="r1"&gt;
<i> </i> Yes
<i> </i> &lt;/label&gt;
<i> </i> &lt;input type="radio" onclick="toggle(elements[0]);" id="r1" name="d" /&gt;
<i> </i> &lt;label for="r2"&gt;
<i> </i> No
<i> </i> &lt;/label&gt;
<i> </i> &lt;input type="radio" onclick="toggle(elements[0]);" id="r2" name="d" /&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;/form&gt;
<i> </i>&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@RavenWindauthorJan 06.2004 — 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
×

Success!

Help @RavenWind spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...