<?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; toggle</title>
	<atom:link href="http://www.annedorko.com/blog/tag/toggle/feed" rel="self" type="application/rss+xml" />
	<link>http://www.annedorko.com</link>
	<description>Help for small business</description>
	<lastBuildDate>Fri, 20 Jan 2012 21:36:14 +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</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[<p><strong>EDIT</strong>: First and foremost I am a small business web consultant. If you are a small business owner or know a small business owner, be sure to check out my new blog series called <strong><a title="Small Business Website Guide for Business Owners" href="http://www.annedorko.com/blog/category/guide/the-small-business-website-guide-for-business-owners" target="_blank">The Small Business Website Guide for Small Business Owners</a></strong> or follow me on <strong><a href="http://twitter.com/annedorko" target="_blank">Twitter</a></strong> or<strong> <a href="https://www.facebook.com/pages/Anne-Dorko/210642982310861" target="_blank">Facebook</a></strong> for free updates and tips on how to create, shape and grow a small business website.</p>

<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 ... <a href="http://www.annedorko.com/blog/toggle-enable-and-disable-a-form-field-with-a-checkbox-javascript-101">Read More &#187;</a>]]></description>
			<content:encoded><![CDATA[<p><span style="color: #ff0000;"><strong>EDIT</strong>: First and foremost I am a small business web consultant. If you are a small business owner or know a small business owner, be sure to check out my new blog series called <strong><a title="Small Business Website Guide for Business Owners" href="http://www.annedorko.com/blog/category/guide/the-small-business-website-guide-for-business-owners" target="_blank">The Small Business Website Guide for Small Business Owners</a></strong> or follow me on <strong><a href="http://twitter.com/annedorko" target="_blank">Twitter</a></strong> or<strong> <a href="https://www.facebook.com/pages/Anne-Dorko/210642982310861" target="_blank">Facebook</a></strong> for free updates and tips on how to create, shape and grow a small business website.</span></p>
<hr />
<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;input
    id="example"
    name="example"
    onClick="toggle('example', 'disableMe')"
    type="checkbox" value="1" /&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>7</slash:comments>
		</item>
	</channel>
</rss>

