<?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; form</title>
	<atom:link href="http://www.randomsnippets.com/tag/form/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.randomsnippets.com</link>
	<description>Random Snippets of Code for Web Developers</description>
	<lastBuildDate>Fri, 25 Nov 2011 23:28:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<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>Allen Liu</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[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! Demo I like to program in: PHP Perl Ruby ASP I like to eat: &#8230; <a href="http://www.randomsnippets.com/2008/05/15/how-to-loop-through-checkboxes-or-radio-button-groups-via-javascript/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></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" 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: #339933;">;</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: #339933;">;</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> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> form.<span style="color: #660066;">elements</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</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: #339933;">;</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: #339933;">;</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<span style="color: #339933;">;</span>
    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: #339933;">;</span>
<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>
<p>If you found that my code was helpful in any way, shape, or form and would like to buy me a <strong>beer</strong>, please use the <strong>Donate</strong> button below =)  Cheers!</p>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="P7VD7JXQGY2YJ">
<input type="image" style="border: none;" src="https://www.paypalobjects.com/WEBSCR-640-20110306-1/en_US/i/btn/btn_donateCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.paypalobjects.com/WEBSCR-640-20110306-1/en_US/i/scr/pixel.gif" width="1" height="1"><br />
</form>
<p></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>16</slash:comments>
		</item>
		<item>
		<title>How to dynamically add content to a div and store the content to a cookie via JavaScript</title>
		<link>http://www.randomsnippets.com/2008/04/14/how-to-dynamically-add-content-to-a-div-via-javascript/</link>
		<comments>http://www.randomsnippets.com/2008/04/14/how-to-dynamically-add-content-to-a-div-via-javascript/#comments</comments>
		<pubDate>Tue, 15 Apr 2008 06:45:17 +0000</pubDate>
		<dc:creator>Allen Liu</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[add]]></category>
		<category><![CDATA[content]]></category>
		<category><![CDATA[cookieContent]]></category>
		<category><![CDATA[demo]]></category>
		<category><![CDATA[demo content]]></category>
		<category><![CDATA[div id]]></category>
		<category><![CDATA[document cookie]]></category>
		<category><![CDATA[document getelementbyid]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[dynamic content]]></category>
		<category><![CDATA[Dynamically]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[getCookie]]></category>
		<category><![CDATA[getElementById]]></category>
		<category><![CDATA[input text]]></category>
		<category><![CDATA[javascript code]]></category>
		<category><![CDATA[javascript function]]></category>
		<category><![CDATA[javascript functions]]></category>
		<category><![CDATA[onClick]]></category>
		<category><![CDATA[setCookie]]></category>
		<category><![CDATA[store content]]></category>

		<guid isPermaLink="false">http://www.randomsnippets.com/?p=14</guid>
		<description><![CDATA[This is an example of adding dynamic content via JavaScript by allowing the user type in the actual content. Demo Content to be added: Type here&#8230; tags work as well woohoo Your content will be added dynamically below: Here is &#8230; <a href="http://www.randomsnippets.com/2008/04/14/how-to-dynamically-add-content-to-a-div-via-javascript/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><script lang="javascript">
function addContent(divName, content) {
     document.getElementById(divName).innerHTML = content;
}
function setCookie(c_name,value,expiredays) {
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
function getCookie(c_name) {
    if (document.cookie.length>0) {
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1) { 
            c_start=c_start + c_name.length+1; 
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1) c_end=document.cookie.length;
            var cookieContent = "Welcome back " + unescape(document.cookie.substring(c_start,c_end));
            document.getElementById('myDiv2').innerHTML = cookieContent;
        } 
    }
}
</script><br />
This is an example of adding dynamic content via JavaScript by allowing the user type in the actual content.</p>
<fieldset>
<legend>Demo</legend>
<form name="myForm">
Content to be added:<br />
<textarea name="myContent">Type here&#8230;<br />
<h1>tags work as well</h1>
<p><u>woohoo</u></textarea></p>
<input type="button" value="Add content" onClick="addContent('myDiv', document.myForm.myContent.value)">
</form>
<p>Your content will be added dynamically below:</p>
<div id="myDiv"></div>
</fieldset>
<p><span id="more-14"></span><br />
Here is the plain HTML for the demo:</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;myForm&quot;&gt;
Content to be added:
&lt;textarea name=&quot;myContent&quot;&gt;Type here...&lt;h1&gt;tags work as well&lt;/h1&gt;&lt;u&gt;woohoo&lt;/u&gt;&lt;/textarea&gt;
&lt;input type=&quot;button&quot; value=&quot;Add content&quot; onClick=&quot;addContent('myDiv', document.myForm.myContent.value); setCookie('content', document.myForm.myContent.value, 7);&quot;&gt;
&lt;/form&gt;
&lt;br&gt;&lt;br&gt;
Your content will be added dynamically below:
&lt;div id=&quot;myDiv&quot;&gt;&lt;/div&gt;</pre></td></tr></table></div>

<p>There are 3 important parts to the HTML code:</p>
<ol>
<li><b>textarea</b> &#8211; This is the content that the user enters.  HTML tags are naturally supported.</li>
<li><b>Add content button</b> &#8211;  This buttons calls our <b>addContent()</b> function which passes the div id of where the content is to be entered and the actual content itself from the <b>textarea</b>.</li>
<li><b>myDiv</b> &#8211; This is the id of blank div where the content will be inserted.</li>
</ol>
<p>Here is the JavaScript code for the <b>addContent()</b> function:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> addContent<span style="color: #009900;">&#40;</span>divName<span style="color: #339933;">,</span> content<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>divName<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> content<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>That&#8217;s it!  All it does it retrieve the DOM element, which in this case, is just an empty div and sets the <b>innerHTML</b> attribute to the content that is passed over to this function from the form.  </p>
<p>This demo is in response to a request that saves the content that the user has entered.</p>
<fieldset>
<legend>Demo</legend>
<form name="myForm2">
Enter your name below and click the button.  Your name will be saved to a cookie that expires in 7 days.  When you refresh the page or come back to visit you will see a heartwarming message welcoming you back.</p>
<input text name="myContent2" value="Enter your name here...">
<input type="button" value="Save my name" onClick="addContent('myDiv2', document.myForm2.myContent2.value); setCookie('content', document.myForm2.myContent2.value, 7);">
</form>
<p>Your name will be saved below:</p>
<div id="myDiv2" style="font-weight: bold;"></div>
</fieldset>
<p>Here is the HTML code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;form name=&quot;myForm2&quot;&gt;
Enter your name below and click the button.  Your name will be saved to a cookie that expires in 7 days.  When you refresh the page or come back to visit you will see a heartwarming message welcoming you back.
&lt;br&gt;&lt;br&gt;
&lt;input text name=&quot;myContent2&quot; value=&quot;Enter your name here...&quot;&gt;
&lt;input type=&quot;button&quot; value=&quot;Save my name&quot; onClick=&quot;addContent('myDiv2', document.myForm2.myContent2.value); setCookie('content', document.myForm2.myContent2.value, 7);&quot;&gt;
&lt;/form&gt;
&lt;br&gt;&lt;br&gt;
Your name will be saved below:
&lt;div id=&quot;myDiv2&quot; style=&quot;font-weight: bold;&quot;&gt;&lt;/div&gt;</pre></td></tr></table></div>

<p>The <b>onClick</b> event listener will launch 2 scripts this time:</p>
<ol>
<li><b>addContent</b> &#8211; This will do exactly what it did before in the first demo and just insert the input text value inside the div.</li>
<li><b>setCookie</b> &#8211; This will save a cookie with the name <b>content</b> and assign whatever is inside the input text as the value.  In addition, the cookie is set to expire in 7 days.</li>
</ol>
<p>To avoid reinventing the wheel, I have borrowed the JavaScript get and set cookie functions from <a href="http://www.w3schools.com/JS/js_cookies.asp" target="_blank">W3 Schools</a> and edited it slightly for the needs of this demo.</p>
<p>Here are the new JavaScript functions for cookie management:</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" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> setCookie<span style="color: #009900;">&#40;</span>c_name<span style="color: #339933;">,</span>value<span style="color: #339933;">,</span>expiredays<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> exdate<span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    exdate.<span style="color: #660066;">setDate</span><span style="color: #009900;">&#40;</span>exdate.<span style="color: #660066;">getDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span>expiredays<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    document.<span style="color: #660066;">cookie</span><span style="color: #339933;">=</span>c_name<span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;=&quot;</span> <span style="color: #339933;">+</span>escape<span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>expiredays<span style="color: #339933;">==</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #3366CC;">&quot;&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;;expires=&quot;</span><span style="color: #339933;">+</span>exdate.<span style="color: #660066;">toGMTString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> getCookie<span style="color: #009900;">&#40;</span>c_name<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>document.<span style="color: #660066;">cookie</span>.<span style="color: #660066;">length</span><span style="color: #339933;">&gt;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        c_start<span style="color: #339933;">=</span>document.<span style="color: #660066;">cookie</span>.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span>c_name <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;=&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>c_start<span style="color: #339933;">!=-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
            c_start<span style="color: #339933;">=</span>c_start <span style="color: #339933;">+</span> c_name.<span style="color: #660066;">length</span><span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span> 
            c_end<span style="color: #339933;">=</span>document.<span style="color: #660066;">cookie</span>.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;;&quot;</span><span style="color: #339933;">,</span>c_start<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>c_end<span style="color: #339933;">==-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> c_end<span style="color: #339933;">=</span>document.<span style="color: #660066;">cookie</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> cookieContent <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Welcome back &quot;</span> <span style="color: #339933;">+</span> unescape<span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">cookie</span>.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span>c_start<span style="color: #339933;">,</span>c_end<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'myDiv2'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> cookieContent<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> 
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
getCookie<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'content'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Please refer to <a href="http://www.w3schools.com/JS/js_cookies.asp" target="_blank">W3 Schools</a> for a detailed description of these JavaScript functions.  The only edit I have made is with the <b>getCookie</b> function in lines 13-14 where it retrieves the cookie value and inserts the content into a div.  Also, line 18 is very important as it calls the <b>getCookie</b> function and looks for the <b>content</b> cookie.  If the cookie exists, the content will be displayed in the <b>myDiv2</b> div.  </p>
<p>When using these JavaScripts, <b> make sure you load them after all your HTML is loaded</b>.  Otherwise, the function would have no place to insert the cookie content.</p>
<p>Although using cookies is a great way to store information, there are a couple of caveats:</p>
<ol>
<li><b>Size limitation</b> &#8211; This depends on the browser but keeping your cookies to a maximum size of 4000 bytes will keep you in the safe zone.</li>
<li><b>Cookie limit per domain</b> &#8211; This also depends on the browser but the general rule is 20 cookies per domain.</li>
</ol>
<p><script lang="javascript">getCookie('content');</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/04/14/how-to-dynamically-add-content-to-a-div-via-javascript/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>How to validate email format via JavaScript</title>
		<link>http://www.randomsnippets.com/2008/04/01/how-to-verify-email-format-via-javascript/</link>
		<comments>http://www.randomsnippets.com/2008/04/01/how-to-verify-email-format-via-javascript/#comments</comments>
		<pubDate>Wed, 02 Apr 2008 05:30:08 +0000</pubDate>
		<dc:creator>Allen Liu</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[checkEmail]]></category>
		<category><![CDATA[emailRegEx]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[javascript demo]]></category>
		<category><![CDATA[javascript email]]></category>
		<category><![CDATA[javascript function]]></category>
		<category><![CDATA[onclick event]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[valid email address]]></category>
		<category><![CDATA[verification]]></category>
		<category><![CDATA[verifyEmail]]></category>

		<guid isPermaLink="false">http://www.randomsnippets.com/?p=13</guid>
		<description><![CDATA[This JavaScript demo verifies that an email address is in the correct format and that the user has typed in the same address in both fields to prevent mistyping of the address. I have borrowed the regex for verifying the &#8230; <a href="http://www.randomsnippets.com/2008/04/01/how-to-verify-email-format-via-javascript/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><script src="/wp-includes/js/verifyEmail.js" language="Javascript" type="text/javascript"></script><br />
This JavaScript demo verifies that an email address is in the correct format and that the user has typed in the same address in both fields to prevent mistyping of the address.  I have <a href="http://www.regular-expressions.info/email.html" target="_blank">borrowed the regex for verifying the correct email format</a> from a different site. </p>
<fieldset>
<legend>Demo</legend>
<form name="myform">
Email:<br />
<input type="text" name="email1">
<p>
<p>
Please type in your email again:<br />
<input type="text" name="email2">
<p>
<input type="button" onclick="verifyEmail();" value="Check Email Address">
</form>
</fieldset>
<p><span id="more-13"></span><br />
Here is the plain HTML of the form.  Please note that I have removed the action attribute because this is only a demo and we are only concerned with the client-side verification of the JavaScript function.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;form name=&quot;myform&quot;&gt;
Email: &lt;input type=&quot;text&quot; name=&quot;email1&quot;&gt;
&lt;p&gt;&lt;p&gt;
Please type in your email again: &lt;input type=&quot;text&quot; name=&quot;email2&quot;&gt;
&lt;p&gt;&lt;p&gt;
&lt;input type=&quot;button&quot; onClick=&quot;verifyEmail();&quot; value=&quot;Check Email Address&quot;&gt;
&lt;/form&gt;</pre></td></tr></table></div>

<p>Nothing exciting here except in line 6 where the <b>onClick</b> event is triggered when the user clicks on the button.  This event will launch the <b>verifyEmail()</b> JavaScript function which will check the user input of the email address.</p>
<p>Here is the code for the <b>verifyEmail()</b> function:</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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> verifyEmail<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #003366; font-weight: bold;">var</span> <span style="color: #000066;">status</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>     
<span style="color: #003366; font-weight: bold;">var</span> emailRegEx <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i</span><span style="color: #339933;">;</span>
     <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">myform</span>.<span style="color: #660066;">email1</span>.<span style="color: #660066;">value</span>.<span style="color: #660066;">search</span><span style="color: #009900;">&#40;</span>emailRegEx<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Please enter a valid email address.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
     <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">myform</span>.<span style="color: #660066;">email1</span>.<span style="color: #660066;">value</span> <span style="color: #339933;">!=</span> document.<span style="color: #660066;">myform</span>.<span style="color: #660066;">email2</span>.<span style="color: #660066;">value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Email addresses do not match.  Please retype them to make sure they are the same.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
     <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Woohoo!  The email address is in the correct format and they are the same.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #000066;">status</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
     <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066;">status</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This function will return a <b>false</b> value if the user does not type in an email address in the correct format or if the user fails to type in the same email address in both fields.  If the email addresses passes both these tests, then the function will return a <b>true</b> value.</p>
<p>The <a href="http://www.regular-expressions.info/email.html" target="_blank">regex for validating the correct email format in line 2 has been borrowed</a> and edited slightly by adding the <b>i</b> flag so that the regex is case-insensitive.</p>
<p>The <b>if</b> block in lines 3-6 checks to see if the email address in the first field passes the regex check by invoking the <b>search()</b> method on our email string value.  This method accepts regex expressions as arguments to check the string and returns the position of the specified value in the string or a <b>-1</b> if no match was found.  If the <b>search()</b> method returns a <b>-1</b>, then an alert would tell the user that an invalid email address has been entered and returns a <b>false</b> value.</p>
<p>Lines 7-10 checks to see if the user has entered the same string in both email fields.  This is done by a simple equality check on the string values.  If the strings are different, an alert warns the user that the email addresses are not the same and returns a <b>false</b> value.</p>
<p>If we have passed the two tests, then <b>else</b> block in lines 11-14 gets executed.  An alert tells the user that he or she has done a good job and the function returns a <b>true</b> value so that you  can continue with the form submission.</p>
<p>Ideally, you would have this function run during the <b>onSubmit</b> attribute for the <b>form</b> tag.  This way, the form will get submitted when the <b>checkEmail()</b> function returns a <b>true</b> value.  Here is an example of how your form would look like:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;form name=&quot;myForm&quot; method=&quot;POST&quot; action=&quot;myTargetFile&quot; onSubmit=&quot;return checkEmail()&quot;&gt;
Email: &lt;input type=&quot;text&quot; name=&quot;email1&quot;&gt;
&lt;p&gt;&lt;p&gt;
Please type in your email again: &lt;input type=&quot;text&quot; name=&quot;email2&quot;&gt;
&lt;p&gt;&lt;p&gt;
&lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt;
&lt;/form&gt;</pre></td></tr></table></div>

<p>If you found that my code was helpful in any way, shape, or form and would like to buy me a <strong>beer</strong>, please use the <strong>Donate</strong> button below =)  Cheers!</p>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="P7VD7JXQGY2YJ">
<input type="image" style="border: none;" src="https://www.paypalobjects.com/WEBSCR-640-20110306-1/en_US/i/btn/btn_donateCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.paypalobjects.com/WEBSCR-640-20110306-1/en_US/i/scr/pixel.gif" width="1" height="1"><br />
</form>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/04/01/how-to-verify-email-format-via-javascript/feed/</wfw:commentRss>
		<slash:comments>12</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>Allen Liu</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[Here is a quick demo of the select all or none JavaScript function that automatically toggles all of your checkboxes in a given form. Demo My favorite programming/scripting language is: Select All &#124; None JavaScript Perl PHP C++ This is &#8230; <a href="http://www.randomsnippets.com/2008/02/28/javascript-to-select-all-or-none-of-the-checkboxes-in-a-form/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></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" 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: #339933;">;</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><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> myForm.<span style="color: #660066;">length</span><span style="color: #339933;">;</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: #339933;">;</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: #339933;">;</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>
		<item>
		<title>Form verification via JavaScript</title>
		<link>http://www.randomsnippets.com/2008/02/27/form-verification-via-javascript/</link>
		<comments>http://www.randomsnippets.com/2008/02/27/form-verification-via-javascript/#comments</comments>
		<pubDate>Wed, 27 Feb 2008 07:27:12 +0000</pubDate>
		<dc:creator>Allen Liu</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[demo]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[email form]]></category>
		<category><![CDATA[error warnings]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[form email]]></category>
		<category><![CDATA[onSubmit]]></category>
		<category><![CDATA[verification]]></category>
		<category><![CDATA[verifyForm]]></category>

		<guid isPermaLink="false">http://www.randomsnippets.com/2008/02/27/form-verification-via-javascript/</guid>
		<description><![CDATA[Are you looking for a simple way to verify a form that you have? The example below demonstrates some of the common techniques used in verifying a form. Test out the form by leaving at least one of the fields &#8230; <a href="http://www.randomsnippets.com/2008/02/27/form-verification-via-javascript/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><script language="javascript">
function verifyForm(form) {
    var userName = form.name.value;
    var userEmail = form.email.value;
    var success = 1;
    if (!userName) {
        document.getElementById("usernameMsg").style.display = "";
        form.name.style.backgroundColor = "yellow";
        form.name.style.border = "3px red solid";
        success = 0;
    }
    else {
        form.name.style.backgroundColor = "";
        form.name.style.border = "";
        document.getElementById("usernameMsg").style.display = "none";
    }
    if (!userEmail) {
        document.getElementById("emailMsg").style.display = "block";
        form.email.style.backgroundColor = "yellow";
        form.email.style.border = "3px red solid";
        success = 0;
    }
    else {
        form.email.style.backgroundColor = "";
        form.email.style.border = "";
        document.getElementById("emailMsg").style.display = "none";
    }
    if(!success) {
        alert("The form is incomplete.  Please read the error message(s).");
        return false;
    }
    else {
        alert("The form was submitted succesfully!");
        return true;
    }
}
</script><br />
Are you looking for a simple way to verify a form that you have?  The example below demonstrates some of the common techniques used in verifying a form.  Test out the form by leaving at least one of the fields blank before you submit the form.  </p>
<fieldset>
<legend>Demo</legend>
<form method="POST" onSubmit="return verifyForm(this);">
Username<br />
<input type="text" name="name">
<div id="usernameMsg" style="display:none;"><span style="color:red;">Please fill in your username.</span></div>
<p>
Email<br />
<input type="email" name="email">
<div id="emailMsg" style="display:none;"><span style="color:red;">Please fill in your email.</span></div>
<p></p>
<input type="submit" value="Submit">
</form>
</fieldset>
<p><span id="more-8"></span><br />
Here is the plain HTML for the form:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;form method=&quot;POST&quot; onSubmit=&quot;return verifyForm(this);&quot;&gt;
Username&lt;input type=&quot;text&quot; name=&quot;name&quot;&gt;&lt;div id=&quot;usernameMsg&quot; style=&quot;display:none;&quot;&gt;&lt;span style=&quot;color:red;&quot;&gt;Please fill in your username.&lt;/span&gt;&lt;/div&gt;
&lt;br&gt;
Email&lt;input type=&quot;email&quot; name=&quot;email&quot;&gt;&lt;div id=&quot;emailMsg&quot; style=&quot;display:none;&quot;&gt;&lt;span style=&quot;color:red;&quot;&gt;Please fill in your email.&lt;/span&gt;&lt;/div&gt;
&lt;br&gt;
&lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt;
&lt;/form&gt;</pre></div></div>

<p>Notice that the error warnings such as the <b>Please fill in your username.</b> or <b>Please fill in your email.</b> have already been preloaded but hidden from the viewer by setting the <code>display</code> attribute to <code>none</code>.  If the user submits the form with one or more of the fields blank, 4 things will happen to notify the user (Note: I tried to squeeze in as many warning events as possible just for demonstration purposes so I apologize if it is a bit over the top):</p>
<ol>
<li>the background of the missing input field will turn yellow</li>
<li>a red border will appear around the missing input field</li>
<li>the hidden error message will become visible</li>
<li>an alert will pop up notifying the user that 1 or more fields are missing</li>
</ol>
<p>When the user clicks on the <b>Submit</b> button, the <b>onSubmit</b> event gets triggered and executes the <b>verifyForm()</b> JavaScript function.  If the <b>onSubmit</b> event returns a <code>false</code> value, the form does not get submitted.  This value is determined by the <b>verifyForm()</b> function, which returns <code>true</code> if all fields are filled in and returns <code>false</code> if one or more fields are missing.   </p>
<p>Here is a peek at the JavaScript <b>verifyForm()</b> function:</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
25
26
27
28
29
30
31
32
33
34
35
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> verifyForm<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> userName <span style="color: #339933;">=</span> form.<span style="color: #000066;">name</span>.<span style="color: #660066;">value</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> userEmail <span style="color: #339933;">=</span> form.<span style="color: #660066;">email</span>.<span style="color: #660066;">value</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> success <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>userName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;usernameMsg&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;block&quot;</span><span style="color: #339933;">;</span>
        form.<span style="color: #000066;">name</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">backgroundColor</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;yellow&quot;</span><span style="color: #339933;">;</span>
        form.<span style="color: #000066;">name</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">border</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;3px red solid&quot;</span><span style="color: #339933;">;</span>
        success <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
        form.<span style="color: #000066;">name</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">backgroundColor</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
        form.<span style="color: #000066;">name</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">border</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
        document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;usernameMsg&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>userEmail<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;emailMsg&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;block&quot;</span><span style="color: #339933;">;</span>
        form.<span style="color: #660066;">email</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">backgroundColor</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;yellow&quot;</span><span style="color: #339933;">;</span>
        form.<span style="color: #660066;">email</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">border</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;3px red solid&quot;</span><span style="color: #339933;">;</span>
        success <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
        form.<span style="color: #660066;">email</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">backgroundColor</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
        form.<span style="color: #660066;">email</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">border</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
        document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;emailMsg&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>success<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;The form is incomplete.  Please read the error message(s).&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;The form was submitted succesfully!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Lines 5-15 checks to see if the username field is blank or not.  If the field is blank, it uses the DOM to access the div that is holding our warning message to the user and sets the <code>display</code> attribute to <code>block</code> (line 6).  This makes the message visible to the user.  </p>
<p>Next, it accesses the input text field and dynamically changes the <code>backgroundColor</code> attribute to <code>yellow</code> (line 7). </p>
<p>Then, it accesses the input text field again and dynamically changes the <code>border</code> attribute to <code>3px red solid</code> (line 8).  Here is a quick breakdown of the attribute we just used:</p>
<ul>
<li>3px &#8211; the width of the border</li>
<li>red &#8211; the color of the border</li>
<li>solid &#8211; the style of the border</li>
</ul>
<p>Finally, it sets the <code>success</code> variable to <code>0</code> which is equivalent to a <code>false</code> value (line 9).  We will use this variable later on to decide if the form is ready to be submitted or not.</p>
<p>The <code>else</code> statement in lines 11-14 just resets all the styling back to the default values.  This is for the case where a user is resubmitting the form so we want to remove all the warnings just in case they were activated from any previous submissions.</p>
<p>Lines 16-26 does the same exact thing except it is for the email text input field.  Remember, all we are doing in this example is checking to see if the field is empty or not.  This is to keep the code simple.</p>
<p>Line 27 checks the <code>success</code> variable to see if it had been set to false.  If so, an alert is executed telling the user that one or more fields were missing and to check the error messages (line 28).  Then it returns a <code>false</code> value to prevent the form from being submitted (line 29).</p>
<p>If the form has been filled-in completely, the <code>else</code> block gets executed in lines 31-34.  In this case, an alert is executed saying that the form was submitted successfully.  As a resutl, the function returns the value <code>true</code> which then submits the form.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/02/27/form-verification-via-javascript/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to dynamically add form elements via JavaScript</title>
		<link>http://www.randomsnippets.com/2008/02/21/how-to-dynamically-add-form-elements-via-javascript/</link>
		<comments>http://www.randomsnippets.com/2008/02/21/how-to-dynamically-add-form-elements-via-javascript/#comments</comments>
		<pubDate>Thu, 21 Feb 2008 08:00:43 +0000</pubDate>
		<dc:creator>Allen Liu</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[appendchild]]></category>
		<category><![CDATA[demo]]></category>
		<category><![CDATA[div element]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[dynamic text]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[input button]]></category>
		<category><![CDATA[input text]]></category>
		<category><![CDATA[javascript function]]></category>
		<category><![CDATA[text element]]></category>
		<category><![CDATA[text input]]></category>
		<category><![CDATA[text inputs]]></category>

		<guid isPermaLink="false">http://www.randomsnippets.com/2008/02/21/how-to-dynamically-add-form-elements-via-javascript/</guid>
		<description><![CDATA[Not all forms are meant to be static. Sometimes, you want to allow the users to add certain parts of the form as they need them. Here is a nice example of dynamically adding inputs to your form as users &#8230; <a href="http://www.randomsnippets.com/2008/02/21/how-to-dynamically-add-form-elements-via-javascript/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><script src="/wp-includes/js/addInput.js" language="Javascript" type="text/javascript"></script><br />
<script src="/wp-includes/js/addAllInputs.js" language="Javascript" type="text/javascript"></script><br />
Not all forms are meant to be static.  Sometimes, you want to allow the users to add certain parts of the form as they need them.  Here is a nice example of dynamically adding inputs to your form as users need them.  In addition, an input limit has been implemented in the script and it is set to 3.</p>
<fieldset>
<legend>Demo</legend>
<form method="POST">
<div id="dynamicInput">
          Entry 1<br />
<input type="text" name="myInputs[]">
     </div>
<input type="button" value="Add another text input" onClick="addInput('dynamicInput');">
</form>
</fieldset>
<p><span id="more-6"></span><br />
Here is the plain HTML for the form:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;script src=&quot;/wp-includes/js/addInput.js&quot; language=&quot;Javascript&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
&lt;form method=&quot;POST&quot;&gt;
     &lt;div id=&quot;dynamicInput&quot;&gt;
          Entry 1&lt;br&gt;&lt;input type=&quot;text&quot; name=&quot;myInputs[]&quot;&gt;
     &lt;/div&gt;
     &lt;input type=&quot;button&quot; value=&quot;Add another text input&quot; onClick=&quot;addInput('dynamicInput');&quot;&gt;
&lt;/form&gt;</pre></div></div>

<p>When the user clicks on the <b>Add another text input</b> button, the <code>addInput</code> JavaScript function is executed and given the <b>dynamicInput</b> argument which is the name of the div that contains all our text inputs.  Here is what our JavaScript function looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> counter <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> limit <span style="color: #339933;">=</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">function</span> addInput<span style="color: #009900;">&#40;</span>divName<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>counter <span style="color: #339933;">==</span> limit<span style="color: #009900;">&#41;</span>  <span style="color: #009900;">&#123;</span>
          <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;You have reached the limit of adding &quot;</span> <span style="color: #339933;">+</span> counter <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; inputs&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
     <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #003366; font-weight: bold;">var</span> newdiv <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          newdiv.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Entry &quot;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>counter <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; &lt;br&gt;&lt;input type='text' name='myInputs[]'&gt;&quot;</span><span style="color: #339933;">;</span>
          document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>divName<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>newdiv<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          counter<span style="color: #339933;">++;</span>
     <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>There are a couple of global variables:</p>
<ul>
<li><code>counter</code> &#8211; Tracks the number inputs being added.</li>
<li><code>limit</code> &#8211; The maximum number inputs allowed for the user to add. In this case, we are limiting the user to a maximum of 3 input texts that they can submit with the form.</li>
</ul>
<p>The <code>addInput</code> JavaScript function expects a parameter, which in this case is the id of the div, to add the dynamic text inputs.  Once executed, the function will first check to see if the maximum number of input texts have already been used.  If so, an alert will be given stating that the limit has been reached.</p>
<p>If the limit has not been reached, then the <code>else</code> block of the code is executed.  A new <code>div</code> element is created and the inner HTML is set with 2 things:</p>
<ul>
<li>Entry # &#8211; The global variable, <code>counter</code>, keeps track of the current number of text inputs that are in the form.</li>
<li>The input text element &#8211; The name of these text inputs,<code>myInputs[]</code>, is followed by square brackets to indicate that it is an array.  This will be more important later on when the form is submitted using the <code>POST</code> method.</li>
</ul>
<p>If the form gets submitted to a PHP file, you can easily access each input value for your <code>myInputs</code> array.  Here is the PHP code for doing just that:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$myInputs</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;myInputs&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$myInputs</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$eachInput</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$eachInput</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is just a sample code to <code>echo</code> all the text input values that were sent from the form.  An array variable, <code>$myInputsPHP</code>, is initialized with the <code>myInputs</code> from the form.  Then, the <code>foreach</code> loop will go through each value and print them out.</p>
<p>Here is another version of this script that adds different types of inputs such as <b>text fields</b>, <b>radio buttons</b>, <b>checkboxes</b>, and <b>textareas</b>.  To simplify things, the limit functionality has been removed.</p>
<fieldset>
<legend>Demo</legend>
<form name="myForm" method="POST">
<div id="dynamicInputs">
          Let&#8217;s start adding some inputs of different types.
     </div>
<select name='inputSelect'>
<option value ="text">text field</option>
<option value ="radio">radio button</option>
<option value ="checkbox">checkbox</option>
<option value ="textarea">textarea</option>
</select>
<input type="button" value="Add selected input" onClick="addAllInputs('dynamicInputs', document.myForm.inputSelect.value);">
</form>
</fieldset>
<p>Here is the plain JavaScript code for the <b>addAllInputs</b> function.  It accepts 2 arguments:</p>
<ul>
<li><b>div id</b> &#8211; This is the div id where all the dynamic inputs will be added to.</li>
<li><b>input type</b> &#8211; This is the type of input that we want to add.</li>
</ul>

<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
25
26
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> counterText <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> counterRadioButton <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> counterCheckBox <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> counterTextArea <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">function</span> addAllInputs<span style="color: #009900;">&#40;</span>divName<span style="color: #339933;">,</span> inputType<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     <span style="color: #003366; font-weight: bold;">var</span> newdiv <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #000066; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span>inputType<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'text'</span><span style="color: #339933;">:</span>
               newdiv.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Entry &quot;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>counterText <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; &lt;br&gt;&lt;input type='text' name='myInputs[]'&gt;&quot;</span><span style="color: #339933;">;</span>
               counterText<span style="color: #339933;">++;</span>
               <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
          <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'radio'</span><span style="color: #339933;">:</span>
               newdiv.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Entry &quot;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>counterRadioButton <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; &lt;br&gt;&lt;input type='radio' name='myRadioButtons[]'&gt;&quot;</span><span style="color: #339933;">;</span>
               counterRadioButton<span style="color: #339933;">++;</span>
               <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
          <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'checkbox'</span><span style="color: #339933;">:</span>
               newdiv.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Entry &quot;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>counterCheckBox <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; &lt;br&gt;&lt;input type='checkbox' name='myCheckBoxes[]'&gt;&quot;</span><span style="color: #339933;">;</span>
               counterCheckBox<span style="color: #339933;">++;</span>
               <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
          <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'textarea'</span><span style="color: #339933;">:</span>
	       newdiv.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Entry &quot;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>counterTextArea <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; &lt;br&gt;&lt;textarea name='myTextAreas[]'&gt;type here...&lt;/textarea&gt;&quot;</span><span style="color: #339933;">;</span>
               counterTextArea<span style="color: #339933;">++;</span>
               <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
          <span style="color: #009900;">&#125;</span>
     document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>divName<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>newdiv<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>If you found that my code was helpful in any way, shape, or form and would like to buy me a <strong>beer</strong>, please use the <strong>Donate</strong> button below =)  Cheers!</p>
<p><center></p>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="WB5GF5WQ6YE5U">
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1"><br />
</form>
<p></center></p>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/02/21/how-to-dynamically-add-form-elements-via-javascript/feed/</wfw:commentRss>
		<slash:comments>107</slash:comments>
		</item>
	</channel>
</rss>

