<?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>Random Snippets &#187; checkboxes</title>
	<atom:link href="http://www.randomsnippets.com/tag/checkboxes/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.randomsnippets.com</link>
	<description>Random Snippets of Code and Ideas for any Web Developer</description>
	<lastBuildDate>Wed, 05 Aug 2009 14:32:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to loop through checkboxes or radio button groups via JavaScript</title>
		<link>http://www.randomsnippets.com/2008/05/15/how-to-loop-through-checkboxes-or-radio-button-groups-via-javascript/</link>
		<comments>http://www.randomsnippets.com/2008/05/15/how-to-loop-through-checkboxes-or-radio-button-groups-via-javascript/#comments</comments>
		<pubDate>Fri, 16 May 2008 05:00:18 +0000</pubDate>
		<dc:creator>Knix</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[checkboxes]]></category>
		<category><![CDATA[dom object]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[javascript code]]></category>
		<category><![CDATA[javascript function]]></category>
		<category><![CDATA[loop through]]></category>
		<category><![CDATA[radio button groups]]></category>

		<guid isPermaLink="false">http://www.randomsnippets.com/?p=17</guid>
		<description><![CDATA[
function loopForm(form) {
    var cbResults = 'Checkboxes: ';
    var radioResults = 'Radio buttons: ';
    for (var i = 0; i < form.elements.length; i++ ) {
        if (form.elements[i].type == 'checkbox') {
          [...]]]></description>
			<content:encoded><![CDATA[<p><script lang="javascript">
function loopForm(form) {
    var cbResults = 'Checkboxes: ';
    var radioResults = 'Radio buttons: ';
    for (var i = 0; i < form.elements.length; i++ ) {
        if (form.elements[i].type == 'checkbox') {
            if (form.elements[i].checked == true) {
                cbResults += form.elements[i].value + ' ';
            }
        }
        if (form.elements[i].type == 'radio') {
            if (form.elements[i].checked == true) {
                radioResults += form.elements[i].value + ' ';
            }
        }
    }
    document.getElementById("cbResults").innerHTML = cbResults;
    document.getElementById("radioResults").innerHTML = radioResults;
}
</script><br />
Do you have a form with checkboxes or radio buttons that you would like to loop through via JavaScript?  This JavaScript function will do just that!</p>
<fieldset>
<legend>Demo</legend>
<form name="thisForm">
I like to program in:<br />
<input type="checkbox" value="PHP" CHECKED>PHP<br />
<input type="checkbox" value="Perl">Perl<br />
<input type="checkbox" value="Ruby">Ruby<br />
<input type="checkbox" value="ASP">ASP<br />
<hr />
I like to eat:<br />
<input type="radio" value="Snickers" name="candy" CHECKED>Snickers<br />
<input type="radio" value="Hershey's" name="candy">Hershey's<br />
<input type="radio" value="M&#038;M's" name="candy">M&#038;M's<br />
<input type="radio" value="Nerds" name="candy">Nerds<br />
<hr />
I like to drink:<br />
<input type="radio" value="Coke" name="drink" CHECKED>Coke<br />
<input type="radio" value="Gatorade" name="drink">Gatorade<br />
<input type="radio" value="Pepsi" name="drink">Pepsi<br />
<input type="radio" value="Milk" name="drink">Milk
<p>
</p>
<input type="button" value="Submit" onclick="loopForm(document.thisForm);">
</form>
<p><div id="cbResults"></div>
<div id="radioResults"></div>
</fieldset>
<p><span id="more-17"></span><br />
Here is the plain HTML:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;form name=&quot;thisForm&quot;&gt;
I like to program in:&lt;p&gt;
&lt;input type=&quot;checkbox&quot; value=&quot;PHP&quot; CHECKED&gt;PHP&lt;p&gt;
&lt;input type=&quot;checkbox&quot; value=&quot;Perl&quot;&gt;Perl&lt;p&gt;
&lt;input type=&quot;checkbox&quot; value=&quot;Ruby&quot;&gt;Ruby&lt;p&gt;
&lt;input type=&quot;checkbox&quot; value=&quot;ASP&quot;&gt;ASP&lt;p&gt;
&lt;hr&gt;
I like to eat:&lt;p&gt;
&lt;input type=&quot;radio&quot; value=&quot;Snickers&quot; name=&quot;candy&quot; CHECKED&gt;Snickers&lt;p&gt;
&lt;input type=&quot;radio&quot; value=&quot;Hershey's&quot; name=&quot;candy&quot;&gt;Hershey's&lt;p&gt;
&lt;input type=&quot;radio&quot; value=&quot;M&amp;M's&quot; name=&quot;candy&quot;&gt;M&amp;M's&lt;p&gt;
&lt;input type=&quot;radio&quot; value=&quot;Nerds&quot; name=&quot;candy&quot;&gt;Nerds&lt;p&gt;
&lt;hr&gt;
I like to drink:&lt;p&gt;
&lt;input type=&quot;radio&quot; value=&quot;Coke&quot; name=&quot;drink&quot; CHECKED&gt;Coke&lt;p&gt;
&lt;input type=&quot;radio&quot; value=&quot;Gatorade&quot; name=&quot;drink&quot;&gt;Gatorade&lt;p&gt;
&lt;input type=&quot;radio&quot; value=&quot;Pepsi&quot; name=&quot;drink&quot;&gt;Pepsi&lt;p&gt;
&lt;input type=&quot;radio&quot; value=&quot;Milk&quot; name=&quot;drink&quot;&gt;Milk&lt;p&gt;
&lt;br&gt;
&lt;input type=&quot;button&quot; value=&quot;Submit&quot; onclick=&quot;loopForm(document.thisForm);&quot;&gt; 
&lt;/form&gt;
&lt;p&gt;
&lt;div id=&quot;cbResults&quot;&gt;&lt;/div&gt;
&lt;div id=&quot;radioResults&quot;&gt;&lt;/div&gt;</pre></td></tr></table></div>

<p>As usual, there is not too much action going on here.  It is just a plain form with some checkboxes and 2 radio button groups called <b>candy</b> and <b>drink</b>.  Radio buttons with the same name will be grouped and allows the users to only select one option out of the entire group.  The submit button will launch the <b>loopForm</b> JavaScript function and the <b>myForm</b> DOM object is passed over as an argument.  There are also 2 divs in lines 23-24 where are checkbox and radio button results will be posted.  Now, on to more exciting things.</p>
<p>Here is the JavaScript code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> loopForm<span style="color: #009900;">&#40;</span>form<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> cbResults <span style="color: #339933;">=</span> <span style="color: #3366CC;">'Checkboxes: '</span>;
    <span style="color: #003366; font-weight: bold;">var</span> radioResults <span style="color: #339933;">=</span> <span style="color: #3366CC;">'Radio buttons: '</span>;
    <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> 0; i <span style="color: #339933;">&lt;</span> form.<span style="color: #660066;">elements</span>.<span style="color: #660066;">length</span>; i<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>form.<span style="color: #660066;">elements</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">type</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'checkbox'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>form.<span style="color: #660066;">elements</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">checked</span> <span style="color: #339933;">==</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                cbResults <span style="color: #339933;">+=</span> form.<span style="color: #660066;">elements</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">value</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">' '</span>;
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>form.<span style="color: #660066;">elements</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">type</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'radio'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>form.<span style="color: #660066;">elements</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">checked</span> <span style="color: #339933;">==</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                radioResults <span style="color: #339933;">+=</span> form.<span style="color: #660066;">elements</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">value</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">' '</span>;
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;cbResults&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> cbResults;
    document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;radioResults&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> radioResults;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This function loops through all the elements in the form that was passed over to the function.  </p>
<p>If the element is of <b>checkbox</b> type and it is checked, the value of this element will be appended to our <b>cbResults</b> variable which is keeping track of our checkbox results.</p>
<p>If the element is of <b>radio</b> type and it is checked, the value of this element will be appended to our <b>radioResults</b> variable which is keeping track of our radio button results.</p>
<p>After all the looping is finished, the results are <b>cbResults</b> and <b>radioResults</b> are inserted into the <b>cbResults div</b> and <b>radioResults div</b> respectively.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/05/15/how-to-loop-through-checkboxes-or-radio-button-groups-via-javascript/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>JavaScript to select all or none of the checkboxes in a form</title>
		<link>http://www.randomsnippets.com/2008/02/28/javascript-to-select-all-or-none-of-the-checkboxes-in-a-form/</link>
		<comments>http://www.randomsnippets.com/2008/02/28/javascript-to-select-all-or-none-of-the-checkboxes-in-a-form/#comments</comments>
		<pubDate>Thu, 28 Feb 2008 07:24:09 +0000</pubDate>
		<dc:creator>Knix</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[checkboxes]]></category>
		<category><![CDATA[demo]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[javascript function]]></category>
		<category><![CDATA[selectToggle]]></category>

		<guid isPermaLink="false">http://www.randomsnippets.com/2008/02/28/javascript-to-select-all-or-none-of-the-checkboxes-in-a-form/</guid>
		<description><![CDATA[
function selectToggle(toggle, form) {
     var myForm = document.forms[form];
     for( var i=0; i < myForm.length; i++ ) { 
          if(toggle) {
               myForm.elements[i].checked = "checked";
  [...]]]></description>
			<content:encoded><![CDATA[<p><script language="javascript">
function selectToggle(toggle, form) {
     var myForm = document.forms[form];
     for( var i=0; i < myForm.length; i++ ) { 
          if(toggle) {
               myForm.elements[i].checked = "checked";
          } 
          else {
               myForm.elements[i].checked = "";
          }
     }
}
</script><br />
Here is a quick demo of the select <b>all</b> or <b>none</b> JavaScript function that automatically toggles all of your checkboxes in a given form.</p>
<fieldset>
<legend>Demo</legend>
<form name="theForm">
My favorite programming/scripting language is:</p>
<p>Select <a href="javascript:selectToggle(true, 'theForm');">All</a> | <a href="javascript:selectToggle(false, 'theForm');">None</a></p>
<p>
<input type="checkbox" name="answers[]" value="javascript">JavaScript</p>
<p>
<input type="checkbox" name="answers[]" value="perl">Perl</p>
<p>
<input type="checkbox" name="answers[]" value="php">PHP</p>
<p>
<input type="checkbox" name="answers[]" value="c++">C++<br />
</form>
</fieldset>
<p><span id="more-9"></span><br />
This is the plain HTML of the form:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;form name=&quot;theForm&quot;&gt;
My favorite programming/scripting language is:&lt;p&gt;
Select &lt;a href=&quot;javascript:selectToggle(true, 'theForm');&quot;&gt;All&lt;/a&gt; | &lt;a href=&quot;javascript:selectToggle(false, 'theForm');&quot;&gt;None&lt;/a&gt;&lt;p&gt;
&lt;input type=&quot;checkbox&quot; name=&quot;answers[]&quot; value=&quot;javascript&quot;&gt;JavaScript&lt;p&gt;
&lt;input type=&quot;checkbox&quot; name=&quot;answers[]&quot; value=&quot;perl&quot;&gt;Perl&lt;p&gt;
&lt;input type=&quot;checkbox&quot; name=&quot;answers[]&quot; value=&quot;php&quot;&gt;PHP&lt;p&gt;
&lt;input type=&quot;checkbox&quot; name=&quot;answers[]&quot; value=&quot;c++&quot;&gt;C++
&lt;/form&gt;</pre></td></tr></table></div>

<p>There is nothing exciting about the HTML code except for line 3 where all the magic happens.  There are 2 links in this line of code:</p>
<ul>
<li><b>All</b> - When the user clicks on this link, it will execute the <b>selectToggle()</b> JavaScript function and passes over 2 arguments: the value to set the checkboxes (<code>true</code> in this case) and the name of the form.</li>
<li><b>None</b> - This link executes the <b>selectToggle()</b> JavaScript function and passes over the <code>false</code> value to uncheck the checkboxes and the name of the form.</li>
</ul>
<p>Here is a peek at the <b>selectToggle()</b> JavaScript function:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> selectToggle<span style="color: #009900;">&#40;</span>toggle<span style="color: #339933;">,</span> form<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #003366; font-weight: bold;">var</span> myForm <span style="color: #339933;">=</span> document.<span style="color: #660066;">forms</span><span style="color: #009900;">&#91;</span>form<span style="color: #009900;">&#93;</span>;
     <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span>0; i <span style="color: #339933;">&lt;</span> myForm.<span style="color: #660066;">length</span>; i<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
          <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>toggle<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
               myForm.<span style="color: #660066;">elements</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">checked</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;checked&quot;</span>;
          <span style="color: #009900;">&#125;</span> 
          <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
               myForm.<span style="color: #660066;">elements</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">checked</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span>;
          <span style="color: #009900;">&#125;</span>
     <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This function basically loops through all of the inputs in the given form name that is passed over as an argument.  Depending on the value of the <code>toggle</code> parameter, which can be <code>true</code> or <code>false</code>, each form element will checked or unchecked respectively.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/02/28/javascript-to-select-all-or-none-of-the-checkboxes-in-a-form/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
