<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Anne Dorko &#187; JavaScript</title>
	<atom:link href="http://www.annedorko.com/blog/category/web/dev/javascript/feed" rel="self" type="application/rss+xml" />
	<link>http://www.annedorko.com</link>
	<description>Web Designer · Graphic Designer · Web Developer</description>
	<lastBuildDate>Tue, 10 Aug 2010 05:29:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Toggle (enable and disable) a form field with a checkbox: JavaScript 101</title>
		<link>http://www.annedorko.com/blog/toggle-enable-and-disable-a-form-field-with-a-checkbox-javascript-101</link>
		<comments>http://www.annedorko.com/blog/toggle-enable-and-disable-a-form-field-with-a-checkbox-javascript-101#comments</comments>
		<pubDate>Thu, 23 Jul 2009 23:09:09 +0000</pubDate>
		<dc:creator>Anne Dorko</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[toggle]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.annedorko.com/?p=188</guid>
		<description><![CDATA[Today at work I was creating a form. I&#8217;m not a big JavaScript buff, but one of my tasks was to use a checkbox to toggle a form field between &#8220;enabled&#8221; and &#8220;disabled&#8221;. So, after browsing the web for a solution, I decided it would be simpler to roll up my sleeves and get dirty [...]]]></description>
			<content:encoded><![CDATA[<p>Today at work I was creating a form. I&#8217;m not a big JavaScript buff, but one of my tasks was to use a checkbox to toggle a form field between &#8220;enabled&#8221; and &#8220;disabled&#8221;. So, after browsing the web for a solution, I decided it would be simpler to roll up my sleeves and get dirty writing my own function.</p>
<p>Here is what I came up with (<a href="http://www.annedorko.com/site/wp-content/uploads/2009/07/demo.html" target="_blank">view the demo</a>):</p>
<h2>JavaScript</h2>
<pre><span style="color: #003366;">function</span> <span style="color: #333333;">toggle</span>(checkboxID, toggleID) {
  <span style="color: #000080;">var</span> checkbox <span style="color: #ff00ff;">=</span> <span style="color: #ff6600;">document</span>.<span style="color: #000000;">getElementById</span>(checkboxID);
  <span style="color: #000080;">var</span> toggle <span style="color: #ff00ff;">=</span> <span style="color: #ff6600;">document</span>.<span style="color: #000000;">getElementById</span>(toggleID);
  updateToggle <span style="color: #ff00ff;">=</span> checkbox.checked ? toggle.disabled<span style="color: #ff00ff;">=</span><span style="color: #3366ff;">true</span> : toggle.disabled<span style="color: #ff00ff;">=</span><span style="color: #3366ff;">false</span>;
}</pre>
<h2>HTML</h2>
<pre>&lt;<span>input</span><span>
    id</span>=<span>"example"
    </span><span>name</span>=<span>"example" </span><span>
    onClick</span>=<span>"toggle('example', 'disableMe')" </span><span>
    type</span>=<span>"checkbox" </span><span>value</span>=<span>"1" </span><span><span>/</span></span>&gt; When this is checked, something is disabled &lt;br /&gt;

&lt;input
    id="disableMe"
    name="disableMe"
    type="text"
    value="This input box will get disabled" /&gt;</pre>
<p>This is pretty basic.</p>
<p>What we are doing is feeding the function the ID of the checkbox field, and the ID of the form field we want to disable. When you check or uncheck the box, we call the toggle() function, which determines what action we took &#8211; if it is checked, we <em>disable</em> the form field, and if it is unchecked, we <em>enable</em> the form field.</p>
<p>If you want to accomplish the opposite (checked = enabled, unchecked = disabled), simply switch the disabled values in the function:</p>
<pre><span style="color: #003366;">function</span> <span style="color: #333333;">toggle</span>(checkboxID, toggleID) {
  <span style="color: #000080;">var</span> checkbox <span style="color: #ff00ff;">=</span> <span style="color: #ff6600;">document</span>.<span style="color: #000000;">getElementById</span>(checkboxID);
  <span style="color: #000080;">var</span> toggle <span style="color: #ff00ff;">=</span> <span style="color: #ff6600;">document</span>.<span style="color: #000000;">getElementById</span>(toggleID);
  updateToggle <span style="color: #ff00ff;">=</span> checkbox.checked ? toggle.disabled<span style="color: #ff00ff;">=</span><span style="color: #3366ff;">false</span> : toggle.disabled<span style="color: #ff00ff;">=</span><span style="color: #3366ff;">true</span>;
}</pre>
<p>I haven&#8217;t done extensive testing as to whether this works across all browsers, but it seems to be working just fine in Firefox.</p>
<p><a href="http://www.annedorko.com/site/wp-content/uploads/2009/07/demo.html">Demo</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.annedorko.com/blog/toggle-enable-and-disable-a-form-field-with-a-checkbox-javascript-101/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
