<?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; demo</title>
	<atom:link href="http://www.randomsnippets.com/tag/demo/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 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>Knix</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[
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 + "=");
     [...]]]></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 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: #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 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>;
    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>;
    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: #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>0<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: #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>; 
            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: #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: #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>;
            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: #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>;</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>9</slash:comments>
		</item>
		<item>
		<title>Simulate a button click via JavaScript</title>
		<link>http://www.randomsnippets.com/2008/03/05/simulate-a-button-click-via-javascript/</link>
		<comments>http://www.randomsnippets.com/2008/03/05/simulate-a-button-click-via-javascript/#comments</comments>
		<pubDate>Wed, 05 Mar 2008 07:09:21 +0000</pubDate>
		<dc:creator>Knix</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[demo]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[javascript alert]]></category>
		<category><![CDATA[javascript check]]></category>
		<category><![CDATA[simulate]]></category>

		<guid isPermaLink="false">http://www.randomsnippets.com/2008/03/05/simulate-a-button-click-via-javascript/</guid>
		<description><![CDATA[There is a rare need for this type of functionality but I have found myself in a couple of situations where I needed it.  Here is a quick demo of a button click that is simulated from another event handler.  In this case, the button click is invoked by checking a checkbox.

Demo
Check the [...]]]></description>
			<content:encoded><![CDATA[<p>There is a rare need for this type of functionality but I have found myself in a couple of situations where I needed it.  Here is a quick demo of a button click that is simulated from another event handler.  In this case, the button click is invoked by checking a checkbox.</p>
<fieldset>
<legend>Demo</legend>
<input type="checkbox" onClick="document.getElementById('theSubmitButton').click();">Check the box to simulate a button click<br />
</p>
<input type="button" name="theSubmitButton" id="theSubmitButton" value="Button" onClick="alert('The button was clicked.');">
</fieldset>
<p><span id="more-10"></span><br />
Here is the HTML code including the JavaScript:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;checkbox&quot;</span> onClick<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;document.getElementById('theSubmitButton').click();&quot;</span><span style="color: #339933;">&gt;</span>Check the box to simulate a button click
<span style="color: #339933;">&lt;</span>br<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;button&quot;</span> <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;theSubmitButton&quot;</span> id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;theSubmitButton&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;Button&quot;</span> onClick<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;alert('The button was clicked.');&quot;</span><span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>If the button is clicked directly, a JavaScript alert will pop up with the following message:</p>
<p><code>The button was clicked.</code></p>
<p>The <strong>onClick</strong> event handler for the checkbox will access the HTML DOM button object and invoke the <code>click()</code> method.  This simulates the button click and invokes the alert.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/03/05/simulate-a-button-click-via-javascript/feed/</wfw:commentRss>
		<slash:comments>19</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>
		<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>Knix</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[
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";
      [...]]]></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 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: #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: #003366; font-weight: bold;">var</span> success <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</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>;
        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>;
        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>;
        success <span style="color: #339933;">=</span> 0;
    <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>;
        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>;
        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: #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>;
        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>;
        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>;
        success <span style="color: #339933;">=</span> 0;
    <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>;
        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>;
        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: #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: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</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: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</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>1</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>Knix</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 need them.  In addition, an input limit has been implemented in the [...]]]></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 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: #003366; font-weight: bold;">var</span> limit <span style="color: #339933;">=</span> <span style="color: #CC0000;">3</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: #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>;
          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>;
          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>;
          counter++;
     <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 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: #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: #990000;">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: #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><br />
          <option value ="radio">radio button</option><br />
          <option value ="checkbox">checkbox</option><br />
          <option value ="textarea">textarea</option><br />
     </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 javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> counterText <span style="color: #339933;">=</span> 0;
<span style="color: #003366; font-weight: bold;">var</span> counterRadioButton <span style="color: #339933;">=</span> 0;
<span style="color: #003366; font-weight: bold;">var</span> counterCheckBox <span style="color: #339933;">=</span> 0;
<span style="color: #003366; font-weight: bold;">var</span> counterTextArea <span style="color: #339933;">=</span> 0;
<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: #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>;
               counterText++;
               <span style="color: #000066; font-weight: bold;">break</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>;
               counterRadioButton++;
               <span style="color: #000066; font-weight: bold;">break</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>;
               counterCheckBox++;
               <span style="color: #000066; font-weight: bold;">break</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>;
               counterTextArea++;
               <span style="color: #000066; font-weight: bold;">break</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: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/02/21/how-to-dynamically-add-form-elements-via-javascript/feed/</wfw:commentRss>
		<slash:comments>65</slash:comments>
		</item>
		<item>
		<title>Dynamic Country State Javascript Menu Example</title>
		<link>http://www.randomsnippets.com/2008/02/09/dynamic-country-state-javascript-menu-example/</link>
		<comments>http://www.randomsnippets.com/2008/02/09/dynamic-country-state-javascript-menu-example/#comments</comments>
		<pubDate>Sat, 09 Feb 2008 07:36:02 +0000</pubDate>
		<dc:creator>Knix</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[demo]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[javascript code]]></category>
		<category><![CDATA[menu]]></category>

		<guid isPermaLink="false">http://www.randomsnippets.com/?p=3</guid>
		<description><![CDATA[
I have found myself in a couple of situations where I needed a simple dynamic javascript menu where the user would make a selection from one select list and depending on what the user selected from the first select list a second list would offer further options.  Here is a perfect example:

Demo
Country:

 Select country&#8230; [...]]]></description>
			<content:encoded><![CDATA[<p><script src="/wp-includes/js/countryToStateMenu.js" language="Javascript" type="text/javascript"></script><br />
I have found myself in a couple of situations where I needed a simple dynamic javascript menu where the user would make a selection from one select list and depending on what the user selected from the first select list a second list would offer further options.  Here is a perfect example:</p>
<fieldset>
<legend>Demo</legend>
<p>Country:<br />
</p>
<select name="country" id="country" style="background-color: #ffffa0" onchange="printStateMenu(this.value);"> <option>Select country&#8230;</option> <option value="US">United  States of America</option> <option value="CA">Canada</option> <option value="AF">Afghanistan</option> <option value="AX">Åland</option> <option value="AL">Albania</option> <option value="DZ">Algeria</option> <option value="AS">American Samoa</option> <option value="AD">Andorra</option> <option value="AO">Angola</option> <option value="AI">Anguilla</option> <option value="AQ">Antarctica</option> <option value="AG">Antigua and Barbuda</option> <option value="AR">Argentina</option> <option value="AM">Armenia</option> <option value="AW">Aruba</option> <option value="AU">Australia</option> <option value="AT">Austria</option> <option value="AZ">Azerbaijan</option> <option value="BS">Bahamas</option> <option value="BH">Bahrain</option> <option value="BD">Bangladesh</option> <option value="BB">Barbados</option> <option value="BY">Belarus</option> <option value="BE">Belgium</option> <option value="BZ">Belize</option> <option value="BJ">Benin</option> <option value="BM">Bermuda</option> <option value="BT">Bhutan</option> <option value="BO">Bolivia</option> <option value="BA">Bosnia and Herzegovina</option> <option value="BW">Botswana</option> <option value="BV">Bouvet Island</option> <option value="BR">Brazil</option> <option value="IO">British Indian Ocean Territory</option> <option value="BN">Brunei Darussalam</option> <option value="BG">Bulgaria</option> <option value="BF">Burkina Faso</option> <option value="BI">Burundi</option> <option value="KH">Cambodia</option> <option value="CM">Cameroon</option> <option value="CV">Cape Verde</option> <option value="KY">Cayman Islands</option> <option value="CF">Central African Republic</option> <option value="TD">Chad</option> <option value="CL">Chile</option> <option value="CN">China</option> <option value="CX">Christmas Island</option> <option value="CC">Cocos (Keeling) Islands</option> <option value="CO">Colombia</option> <option value="KM">Comoros</option> <option value="CG">Congo (Brazzaville)</option> <option value="CD">Congo (Kinshasa)</option> <option value="CK">Cook Islands</option> <option value="CR">Costa Rica</option> <option value="CI">Côte d&#8217;Ivoire</option> <option value="HR">Croatia</option> <option value="CU">Cuba</option> <option value="CY">Cyprus</option> <option value="CZ">Czech Republic</option> <option value="DK">Denmark</option> <option value="DJ">Djibouti</option> <option value="DM">Dominica</option> <option value="DO">Dominican Republic</option> <option value="EC">Ecuador</option> <option value="EG">Egypt</option> <option value="SV">El Salvador</option> <option value="GQ">Equatorial Guinea</option> <option value="ER">Eritrea</option> <option value="EE">Estonia</option> <option value="ET">Ethiopia</option> <option value="FK">Falkland Islands</option> <option value="FO">Faroe Islands</option> <option value="FJ">Fiji</option> <option value="FI">Finland</option> <option value="FR">France</option> <option value="GF">French Guiana</option> <option value="PF">French Polynesia</option> <option value="TF">French Southern Lands</option> <option value="GA">Gabon</option> <option value="GM">Gambia</option> <option value="GE">Georgia</option> <option value="DE">Germany</option> <option value="GH">Ghana</option> <option value="GI">Gibraltar</option> <option value="GR">Greece</option> <option value="GL">Greenland</option> <option value="GD">Grenada</option> <option value="GP">Guadeloupe</option> <option value="GU">Guam</option> <option value="GT">Guatemala</option> <option value="GG">Guernsey</option> <option value="GN">Guinea</option> <option value="GW">Guinea-Bissau</option> <option value="GY">Guyana</option> <option value="HT">Haiti</option> <option value="HM">Heard and McDonald Islands</option> <option value="HN">Honduras</option> <option value="HK">Hong Kong</option> <option value="HU">Hungary</option> <option value="IS">Iceland</option> <option value="IN">India</option> <option value="ID">Indonesia</option> <option value="IR">Iran</option> <option value="IQ">Iraq</option> <option value="IE">Ireland</option> <option value="IM">Isle of Man</option> <option value="IL">Israel</option> <option value="IT">Italy</option> <option value="JM">Jamaica</option> <option value="JP">Japan</option> <option value="JE">Jersey</option> <option value="JO">Jordan</option> <option value="KZ">Kazakhstan</option> <option value="KE">Kenya</option> <option value="KI">Kiribati</option> <option value="KP">Korea ( North )</option> <option value="KR">Korea ( South )</option> <option value="KW">Kuwait</option> <option value="KG">Kyrgyzstan</option> <option value="LA">Laos</option> <option value="LV">Latvia</option> <option value="LB">Lebanon</option> <option value="LS">Lesotho</option> <option value="LR">Liberia</option> <option value="LY">Libya</option> <option value="LI">Liechtenstein</option> <option value="LT">Lithuania</option> <option value="LU">Luxembourg</option> <option value="MO">Macau</option> <option value="MK">Macedonia</option> <option value="MG">Madagascar</option> <option value="MW">Malawi</option> <option value="MY">Malaysia</option> <option value="MV">Maldives</option> <option value="ML">Mali</option> <option value="MT">Malta</option> <option value="MH">Marshall Islands</option> <option value="MQ">Martinique</option> <option value="MR">Mauritania</option> <option value="MU">Mauritius</option> <option value="YT">Mayotte</option> <option value="MX">Mexico</option> <option value="FM">Micronesia</option> <option value="MD">Moldova</option> <option value="MC">Monaco</option> <option value="MN">Mongolia</option> <option value="ME">Montenegro</option> <option value="MS">Montserrat</option> <option value="MA">Morocco</option> <option value="MZ">Mozambique</option> <option value="MM">Myanmar</option> <option value="NA">Namibia</option> <option value="NR">Nauru</option> <option value="NP">Nepal</option> <option value="NL">Netherlands</option> <option value="AN">Netherlands Antilles</option> <option value="NC">New Caledonia</option> <option value="NZ">New Zealand</option> <option value="NI">Nicaragua</option> <option value="NE">Niger</option> <option value="NG">Nigeria</option> <option value="NU">Niue</option> <option value="NF">Norfolk Island</option> <option value="MP">Northern Mariana Islands</option> <option value="NO">Norway</option> <option value="OM">Oman</option> <option value="PK">Pakistan</option> <option value="PW">Palau</option> <option value="PS">Palestine</option> <option value="PA">Panama</option> <option value="PG">Papua New Guinea</option> <option value="PY">Paraguay</option> <option value="PE">Peru</option> <option value="PH">Philippines</option> <option value="PN">Pitcairn</option> <option value="PL">Poland</option> <option value="PT">Portugal</option> <option value="PR">Puerto Rico</option> <option value="QA">Qatar</option> <option value="RE">Reunion</option> <option value="RO">Romania</option> <option value="RU">Russian Federation</option> <option value="RW">Rwanda</option> <option value="BL">Saint Barthélemy</option> <option value="SH">Saint Helena</option> <option value="KN">Saint Kitts and Nevis</option> <option value="LC">Saint Lucia</option> <option value="MF">Saint Martin (French part)</option> <option value="PM">Saint Pierre and Miquelon</option> <option value="VC">Saint Vincent and the Grenadines</option> <option value="WS">Samoa</option> <option value="SM">San Marino</option> <option value="ST">Sao Tome and Principe</option> <option value="SA">Saudi Arabia</option> <option value="SN">Senegal</option> <option value="RS">Serbia</option> <option value="SC">Seychelles</option> <option value="SL">Sierra Leone</option> <option value="SG">Singapore</option> <option value="SK">Slovakia</option> <option value="SI">Slovenia</option> <option value="SB">Solomon Islands</option> <option value="SO">Somalia</option> <option value="ZA">South Africa</option> <option value="GS">South Georgia and South Sandwich Islands</option> <option value="KR">South Korea</option> <option value="ES">Spain</option> <option value="LK">Sri Lanka</option> <option value="SD">Sudan</option> <option value="SR">Suriname</option> <option value="SJ">Svalbard and Jan Mayen Islands</option> <option value="SZ">Swaziland</option> <option value="SE">Sweden</option> <option value="CH">Switzerland</option> <option value="SY">Syria</option> <option value="TW">Taiwan</option> <option value="TJ">Tajikistan</option> <option value="TZ">Tanzania</option> <option value="TH">Thailand</option> <option value="TL">Timor-Leste</option> <option value="TG">Togo</option> <option value="TK">Tokelau</option> <option value="TO">Tonga</option> <option value="TT">Trinidad and Tobago</option> <option value="TN">Tunisia</option> <option value="TR">Turkey</option> <option value="TM">Turkmenistan</option> <option value="TC">Turks and Caicos Islands</option> <option value="TV">Tuvalu</option> <option value="UG">Uganda</option> <option value="UA">Ukraine</option> <option value="AE">United Arab Emirates</option> <option value="GB">United Kingdom</option> <option value="UM">United States Minor Outlying Islands</option> <option value="UY">Uruguay</option> <option value="UZ">Uzbekistan</option> <option value="VU">Vanuatu</option> <option value="VA">Vatican City</option> <option value="VE">Venezuela</option> <option value="VN">Vietnam</option> <option value="VG">Virgin Islands ( British )</option> <option value="VI">Virgin Islands ( U.S. )</option> <option value="WF">Wallis and Futuna Islands</option> <option value="EH">Western Sahara</option> <option value="YE">Yemen</option> <option value="ZM">Zambia</option> <option value="ZW">Zimbabwe</option> </select>
<p>State/Province:</p>
<p id="stateSelect">
<select name="state" disabled="disabled"><option>Select state&#8230;</option></select>
</p>
<table>
<tr>
<td><strong>Country value:</strong></td>
<td>
<p id="countryValue">&nbsp;</p>
</td>
</tr>
<tr>
<td><strong>State/Province value:</strong></td>
<td>
<div id="stateValue">&nbsp;</div>
</td>
</tr>
</table>
</fieldset>
<p><span id="more-3"></span></p>
<p>In this example, the state/province menu is disabled until the user has first selected a country.  Then depending on the country that is chosen, the list of associated states or provinces would follow.  For the sake of simplicity, only the state/province menus have been implemented for the United States and Canada.  If any other country is chosen, the value for the <strong>state</strong> input defaults to <strong>Other</strong>.</p>
<p>Here is the HTML code for the example:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;script src=&quot;/path to JavaScript File/countryToStateMenu.js&quot; language=&quot;Javascript&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
&lt;p&gt;Country:&lt;/p&gt;
&lt;select name=&quot;country&quot; id=&quot;country&quot; onChange=&quot;printStateMenu(this.value);&quot;&gt;'.
     '&lt;option&gt;Select country...&lt;/option&gt;'.
     '&lt;option value=&quot;US&quot;&gt;United States of America&lt;/option&gt;'.
     '&lt;option value=&quot;CA&quot;&gt;Canada&lt;/option&gt;'.
     '&lt;option value=&quot;AF&quot;&gt;Afghanistan&lt;/option&gt;'.
     '&lt;option value=&quot;AX&quot;&gt;Åland&lt;/option&gt;'.
     '&lt;option value=&quot;AL&quot;&gt;Albania&lt;/option&gt;'.
     '&lt;option value=&quot;DZ&quot;&gt;Algeria&lt;/option&gt;'.
     '&lt;option value=&quot;AS&quot;&gt;American Samoa&lt;/option&gt;'.
     '&lt;option value=&quot;AD&quot;&gt;Andorra&lt;/option&gt;'.
     '&lt;option value=&quot;AO&quot;&gt;Angola&lt;/option&gt;'.
     '&lt;option value=&quot;AI&quot;&gt;Anguilla&lt;/option&gt;'.
     '&lt;option value=&quot;AQ&quot;&gt;Antarctica&lt;/option&gt;'.
     '&lt;option value=&quot;AG&quot;&gt;Antigua and Barbuda&lt;/option&gt;'.
     '&lt;option value=&quot;AR&quot;&gt;Argentina&lt;/option&gt;'.
     '&lt;option value=&quot;AM&quot;&gt;Armenia&lt;/option&gt;'.
     '&lt;option value=&quot;AW&quot;&gt;Aruba&lt;/option&gt;'.
     '&lt;option value=&quot;AU&quot;&gt;Australia&lt;/option&gt;'.
     '&lt;option value=&quot;AT&quot;&gt;Austria&lt;/option&gt;'.
     '&lt;option value=&quot;AZ&quot;&gt;Azerbaijan&lt;/option&gt;'.
     '&lt;option value=&quot;BS&quot;&gt;Bahamas&lt;/option&gt;'.
     '&lt;option value=&quot;BH&quot;&gt;Bahrain&lt;/option&gt;'.
     '&lt;option value=&quot;BD&quot;&gt;Bangladesh&lt;/option&gt;'.
     '&lt;option value=&quot;BB&quot;&gt;Barbados&lt;/option&gt;'.
     '&lt;option value=&quot;BY&quot;&gt;Belarus&lt;/option&gt;'.
     '&lt;option value=&quot;BE&quot;&gt;Belgium&lt;/option&gt;'.
     '&lt;option value=&quot;BZ&quot;&gt;Belize&lt;/option&gt;'.
     '&lt;option value=&quot;BJ&quot;&gt;Benin&lt;/option&gt;'.
     '&lt;option value=&quot;BM&quot;&gt;Bermuda&lt;/option&gt;'.
     '&lt;option value=&quot;BT&quot;&gt;Bhutan&lt;/option&gt;'.
     '&lt;option value=&quot;BO&quot;&gt;Bolivia&lt;/option&gt;'.
     '&lt;option value=&quot;BA&quot;&gt;Bosnia and Herzegovina&lt;/option&gt;'.
     '&lt;option value=&quot;BW&quot;&gt;Botswana&lt;/option&gt;'.
     '&lt;option value=&quot;BV&quot;&gt;Bouvet Island&lt;/option&gt;'.
     '&lt;option value=&quot;BR&quot;&gt;Brazil&lt;/option&gt;'.
     '&lt;option value=&quot;IO&quot;&gt;British Indian Ocean Territory&lt;/option&gt;'.
     '&lt;option value=&quot;BN&quot;&gt;Brunei Darussalam&lt;/option&gt;'.
     '&lt;option value=&quot;BG&quot;&gt;Bulgaria&lt;/option&gt;'.
     '&lt;option value=&quot;BF&quot;&gt;Burkina Faso&lt;/option&gt;'.
     '&lt;option value=&quot;BI&quot;&gt;Burundi&lt;/option&gt;'.
     '&lt;option value=&quot;KH&quot;&gt;Cambodia&lt;/option&gt;'.
     '&lt;option value=&quot;CM&quot;&gt;Cameroon&lt;/option&gt;'.
     '&lt;option value=&quot;CV&quot;&gt;Cape Verde&lt;/option&gt;'.
     '&lt;option value=&quot;KY&quot;&gt;Cayman Islands&lt;/option&gt;'.
     '&lt;option value=&quot;CF&quot;&gt;Central African Republic&lt;/option&gt;'.
     '&lt;option value=&quot;TD&quot;&gt;Chad&lt;/option&gt;'.
     '&lt;option value=&quot;CL&quot;&gt;Chile&lt;/option&gt;'.
     '&lt;option value=&quot;CN&quot;&gt;China&lt;/option&gt;'.
     '&lt;option value=&quot;CX&quot;&gt;Christmas Island&lt;/option&gt;'.
     '&lt;option value=&quot;CC&quot;&gt;Cocos (Keeling) Islands&lt;/option&gt;'.
     '&lt;option value=&quot;CO&quot;&gt;Colombia&lt;/option&gt;'.
     '&lt;option value=&quot;KM&quot;&gt;Comoros&lt;/option&gt;'.
     '&lt;option value=&quot;CG&quot;&gt;Congo (Brazzaville)&lt;/option&gt;'.
     '&lt;option value=&quot;CD&quot;&gt;Congo (Kinshasa)&lt;/option&gt;'.
     '&lt;option value=&quot;CK&quot;&gt;Cook Islands&lt;/option&gt;'.
     '&lt;option value=&quot;CR&quot;&gt;Costa Rica&lt;/option&gt;'.
     '&lt;option value=&quot;CI&quot;&gt;Côte dIvoire&lt;/option&gt;'.
     '&lt;option value=&quot;HR&quot;&gt;Croatia&lt;/option&gt;'.
     '&lt;option value=&quot;CU&quot;&gt;Cuba&lt;/option&gt;'.
     '&lt;option value=&quot;CY&quot;&gt;Cyprus&lt;/option&gt;'.
     '&lt;option value=&quot;CZ&quot;&gt;Czech Republic&lt;/option&gt;'.
     '&lt;option value=&quot;DK&quot;&gt;Denmark&lt;/option&gt;'.
     '&lt;option value=&quot;DJ&quot;&gt;Djibouti&lt;/option&gt;'.
     '&lt;option value=&quot;DM&quot;&gt;Dominica&lt;/option&gt;'.
     '&lt;option value=&quot;DO&quot;&gt;Dominican Republic&lt;/option&gt;'.
     '&lt;option value=&quot;EC&quot;&gt;Ecuador&lt;/option&gt;'.
     '&lt;option value=&quot;EG&quot;&gt;Egypt&lt;/option&gt;'.
     '&lt;option value=&quot;SV&quot;&gt;El Salvador&lt;/option&gt;'.
     '&lt;option value=&quot;GQ&quot;&gt;Equatorial Guinea&lt;/option&gt;'.
     '&lt;option value=&quot;ER&quot;&gt;Eritrea&lt;/option&gt;'.
     '&lt;option value=&quot;EE&quot;&gt;Estonia&lt;/option&gt;'.
     '&lt;option value=&quot;ET&quot;&gt;Ethiopia&lt;/option&gt;'.
     '&lt;option value=&quot;FK&quot;&gt;Falkland Islands&lt;/option&gt;'.
     '&lt;option value=&quot;FO&quot;&gt;Faroe Islands&lt;/option&gt;'.
     '&lt;option value=&quot;FJ&quot;&gt;Fiji&lt;/option&gt;'.
     '&lt;option value=&quot;FI&quot;&gt;Finland&lt;/option&gt;'.
     '&lt;option value=&quot;FR&quot;&gt;France&lt;/option&gt;'.
     '&lt;option value=&quot;GF&quot;&gt;French Guiana&lt;/option&gt;'.
     '&lt;option value=&quot;PF&quot;&gt;French Polynesia&lt;/option&gt;'.
     '&lt;option value=&quot;TF&quot;&gt;French Southern Lands&lt;/option&gt;'.
     '&lt;option value=&quot;GA&quot;&gt;Gabon&lt;/option&gt;'.
     '&lt;option value=&quot;GM&quot;&gt;Gambia&lt;/option&gt;'.
     '&lt;option value=&quot;GE&quot;&gt;Georgia&lt;/option&gt;'.
     '&lt;option value=&quot;DE&quot;&gt;Germany&lt;/option&gt;'.
     '&lt;option value=&quot;GH&quot;&gt;Ghana&lt;/option&gt;'.
     '&lt;option value=&quot;GI&quot;&gt;Gibraltar&lt;/option&gt;'.
     '&lt;option value=&quot;GR&quot;&gt;Greece&lt;/option&gt;'.
     '&lt;option value=&quot;GL&quot;&gt;Greenland&lt;/option&gt;'.
     '&lt;option value=&quot;GD&quot;&gt;Grenada&lt;/option&gt;'.
     '&lt;option value=&quot;GP&quot;&gt;Guadeloupe&lt;/option&gt;'.
     '&lt;option value=&quot;GU&quot;&gt;Guam&lt;/option&gt;'.
     '&lt;option value=&quot;GT&quot;&gt;Guatemala&lt;/option&gt;'.
     '&lt;option value=&quot;GG&quot;&gt;Guernsey&lt;/option&gt;'.
     '&lt;option value=&quot;GN&quot;&gt;Guinea&lt;/option&gt;'.
     '&lt;option value=&quot;GW&quot;&gt;Guinea-Bissau&lt;/option&gt;'.
     '&lt;option value=&quot;GY&quot;&gt;Guyana&lt;/option&gt;'.
     '&lt;option value=&quot;HT&quot;&gt;Haiti&lt;/option&gt;'.
     '&lt;option value=&quot;HM&quot;&gt;Heard and McDonald Islands&lt;/option&gt;'.
     '&lt;option value=&quot;HN&quot;&gt;Honduras&lt;/option&gt;'.
     '&lt;option value=&quot;HK&quot;&gt;Hong Kong&lt;/option&gt;'.
     '&lt;option value=&quot;HU&quot;&gt;Hungary&lt;/option&gt;'.
     '&lt;option value=&quot;IS&quot;&gt;Iceland&lt;/option&gt;'.
     '&lt;option value=&quot;IN&quot;&gt;India&lt;/option&gt;'.
     '&lt;option value=&quot;ID&quot;&gt;Indonesia&lt;/option&gt;'.
     '&lt;option value=&quot;IR&quot;&gt;Iran&lt;/option&gt;'.
     '&lt;option value=&quot;IQ&quot;&gt;Iraq&lt;/option&gt;'.
     '&lt;option value=&quot;IE&quot;&gt;Ireland&lt;/option&gt;'.
     '&lt;option value=&quot;IM&quot;&gt;Isle of Man&lt;/option&gt;'.
     '&lt;option value=&quot;IL&quot;&gt;Israel&lt;/option&gt;'.
     '&lt;option value=&quot;IT&quot;&gt;Italy&lt;/option&gt;'.
     '&lt;option value=&quot;JM&quot;&gt;Jamaica&lt;/option&gt;'.
     '&lt;option value=&quot;JP&quot;&gt;Japan&lt;/option&gt;'.
     '&lt;option value=&quot;JE&quot;&gt;Jersey&lt;/option&gt;'.
     '&lt;option value=&quot;JO&quot;&gt;Jordan&lt;/option&gt;'.
     '&lt;option value=&quot;KZ&quot;&gt;Kazakhstan&lt;/option&gt;'.
     '&lt;option value=&quot;KE&quot;&gt;Kenya&lt;/option&gt;'.
     '&lt;option value=&quot;KI&quot;&gt;Kiribati&lt;/option&gt;'.
     '&lt;option value=&quot;KP&quot;&gt;Korea ( North )&lt;/option&gt;'.
     '&lt;option value=&quot;KR&quot;&gt;Korea ( South )&lt;/option&gt;'.
     '&lt;option value=&quot;KW&quot;&gt;Kuwait&lt;/option&gt;'.
     '&lt;option value=&quot;KG&quot;&gt;Kyrgyzstan&lt;/option&gt;'.
     '&lt;option value=&quot;LA&quot;&gt;Laos&lt;/option&gt;'.
     '&lt;option value=&quot;LV&quot;&gt;Latvia&lt;/option&gt;'.
     '&lt;option value=&quot;LB&quot;&gt;Lebanon&lt;/option&gt;'.
     '&lt;option value=&quot;LS&quot;&gt;Lesotho&lt;/option&gt;'.
     '&lt;option value=&quot;LR&quot;&gt;Liberia&lt;/option&gt;'.
     '&lt;option value=&quot;LY&quot;&gt;Libya&lt;/option&gt;'.
     '&lt;option value=&quot;LI&quot;&gt;Liechtenstein&lt;/option&gt;'.
     '&lt;option value=&quot;LT&quot;&gt;Lithuania&lt;/option&gt;'.
     '&lt;option value=&quot;LU&quot;&gt;Luxembourg&lt;/option&gt;'.
     '&lt;option value=&quot;MO&quot;&gt;Macau&lt;/option&gt;'.
     '&lt;option value=&quot;MK&quot;&gt;Macedonia&lt;/option&gt;'.
     '&lt;option value=&quot;MG&quot;&gt;Madagascar&lt;/option&gt;'.
     '&lt;option value=&quot;MW&quot;&gt;Malawi&lt;/option&gt;'.
     '&lt;option value=&quot;MY&quot;&gt;Malaysia&lt;/option&gt;'.
     '&lt;option value=&quot;MV&quot;&gt;Maldives&lt;/option&gt;'.
     '&lt;option value=&quot;ML&quot;&gt;Mali&lt;/option&gt;'.
     '&lt;option value=&quot;MT&quot;&gt;Malta&lt;/option&gt;'.
     '&lt;option value=&quot;MH&quot;&gt;Marshall Islands&lt;/option&gt;'.
     '&lt;option value=&quot;MQ&quot;&gt;Martinique&lt;/option&gt;'.
     '&lt;option value=&quot;MR&quot;&gt;Mauritania&lt;/option&gt;'.
     '&lt;option value=&quot;MU&quot;&gt;Mauritius&lt;/option&gt;'.
     '&lt;option value=&quot;YT&quot;&gt;Mayotte&lt;/option&gt;'.
     '&lt;option value=&quot;MX&quot;&gt;Mexico&lt;/option&gt;'.
     '&lt;option value=&quot;FM&quot;&gt;Micronesia&lt;/option&gt;'.
     '&lt;option value=&quot;MD&quot;&gt;Moldova&lt;/option&gt;'.
     '&lt;option value=&quot;MC&quot;&gt;Monaco&lt;/option&gt;'.
     '&lt;option value=&quot;MN&quot;&gt;Mongolia&lt;/option&gt;'.
     '&lt;option value=&quot;ME&quot;&gt;Montenegro&lt;/option&gt;'.
     '&lt;option value=&quot;MS&quot;&gt;Montserrat&lt;/option&gt;'.
     '&lt;option value=&quot;MA&quot;&gt;Morocco&lt;/option&gt;'.
     '&lt;option value=&quot;MZ&quot;&gt;Mozambique&lt;/option&gt;'.
     '&lt;option value=&quot;MM&quot;&gt;Myanmar&lt;/option&gt;'.
     '&lt;option value=&quot;NA&quot;&gt;Namibia&lt;/option&gt;'.
     '&lt;option value=&quot;NR&quot;&gt;Nauru&lt;/option&gt;'.
     '&lt;option value=&quot;NP&quot;&gt;Nepal&lt;/option&gt;'.
     '&lt;option value=&quot;NL&quot;&gt;Netherlands&lt;/option&gt;'.
     '&lt;option value=&quot;AN&quot;&gt;Netherlands Antilles&lt;/option&gt;'.
     '&lt;option value=&quot;NC&quot;&gt;New Caledonia&lt;/option&gt;'.
     '&lt;option value=&quot;NZ&quot;&gt;New Zealand&lt;/option&gt;'.
     '&lt;option value=&quot;NI&quot;&gt;Nicaragua&lt;/option&gt;'.
     '&lt;option value=&quot;NE&quot;&gt;Niger&lt;/option&gt;'.
     '&lt;option value=&quot;NG&quot;&gt;Nigeria&lt;/option&gt;'.
     '&lt;option value=&quot;NU&quot;&gt;Niue&lt;/option&gt;'.
     '&lt;option value=&quot;NF&quot;&gt;Norfolk Island&lt;/option&gt;'.
     '&lt;option value=&quot;MP&quot;&gt;Northern Mariana Islands&lt;/option&gt;'.
     '&lt;option value=&quot;NO&quot;&gt;Norway&lt;/option&gt;'.
     '&lt;option value=&quot;OM&quot;&gt;Oman&lt;/option&gt;'.
     '&lt;option value=&quot;PK&quot;&gt;Pakistan&lt;/option&gt;'.
     '&lt;option value=&quot;PW&quot;&gt;Palau&lt;/option&gt;'.
     '&lt;option value=&quot;PS&quot;&gt;Palestine&lt;/option&gt;'.
     '&lt;option value=&quot;PA&quot;&gt;Panama&lt;/option&gt;'.
     '&lt;option value=&quot;PG&quot;&gt;Papua New Guinea&lt;/option&gt;'.
     '&lt;option value=&quot;PY&quot;&gt;Paraguay&lt;/option&gt;'.
     '&lt;option value=&quot;PE&quot;&gt;Peru&lt;/option&gt;'.
     '&lt;option value=&quot;PH&quot;&gt;Philippines&lt;/option&gt;'.
     '&lt;option value=&quot;PN&quot;&gt;Pitcairn&lt;/option&gt;'.
     '&lt;option value=&quot;PL&quot;&gt;Poland&lt;/option&gt;'.
     '&lt;option value=&quot;PT&quot;&gt;Portugal&lt;/option&gt;'.
     '&lt;option value=&quot;PR&quot;&gt;Puerto Rico&lt;/option&gt;'.
     '&lt;option value=&quot;QA&quot;&gt;Qatar&lt;/option&gt;'.
     '&lt;option value=&quot;RE&quot;&gt;Reunion&lt;/option&gt;'.
     '&lt;option value=&quot;RO&quot;&gt;Romania&lt;/option&gt;'.
     '&lt;option value=&quot;RU&quot;&gt;Russian Federation&lt;/option&gt;'.
     '&lt;option value=&quot;RW&quot;&gt;Rwanda&lt;/option&gt;'.
     '&lt;option value=&quot;BL&quot;&gt;Saint Barthélemy&lt;/option&gt;'.
     '&lt;option value=&quot;SH&quot;&gt;Saint Helena&lt;/option&gt;'.
     '&lt;option value=&quot;KN&quot;&gt;Saint Kitts and Nevis&lt;/option&gt;'.
     '&lt;option value=&quot;LC&quot;&gt;Saint Lucia&lt;/option&gt;'.
     '&lt;option value=&quot;MF&quot;&gt;Saint Martin (French part)&lt;/option&gt;'.
     '&lt;option value=&quot;PM&quot;&gt;Saint Pierre and Miquelon&lt;/option&gt;'.
     '&lt;option value=&quot;VC&quot;&gt;Saint Vincent and the Grenadines&lt;/option&gt;'.
     '&lt;option value=&quot;WS&quot;&gt;Samoa&lt;/option&gt;'.
     '&lt;option value=&quot;SM&quot;&gt;San Marino&lt;/option&gt;'.
     '&lt;option value=&quot;ST&quot;&gt;Sao Tome and Principe&lt;/option&gt;'.
     '&lt;option value=&quot;SA&quot;&gt;Saudi Arabia&lt;/option&gt;'.
     '&lt;option value=&quot;SN&quot;&gt;Senegal&lt;/option&gt;'.
     '&lt;option value=&quot;RS&quot;&gt;Serbia&lt;/option&gt;'.
     '&lt;option value=&quot;SC&quot;&gt;Seychelles&lt;/option&gt;'.
     '&lt;option value=&quot;SL&quot;&gt;Sierra Leone&lt;/option&gt;'.
     '&lt;option value=&quot;SG&quot;&gt;Singapore&lt;/option&gt;'.
     '&lt;option value=&quot;SK&quot;&gt;Slovakia&lt;/option&gt;'.
     '&lt;option value=&quot;SI&quot;&gt;Slovenia&lt;/option&gt;'.
     '&lt;option value=&quot;SB&quot;&gt;Solomon Islands&lt;/option&gt;'.
     '&lt;option value=&quot;SO&quot;&gt;Somalia&lt;/option&gt;'.
     '&lt;option value=&quot;ZA&quot;&gt;South Africa&lt;/option&gt;'.
     '&lt;option value=&quot;GS&quot;&gt;South Georgia and South Sandwich Islands&lt;/option&gt;'.
     '&lt;option value=&quot;KR&quot;&gt;South Korea&lt;/option&gt;'.
     '&lt;option value=&quot;ES&quot;&gt;Spain&lt;/option&gt;'.
     '&lt;option value=&quot;LK&quot;&gt;Sri Lanka&lt;/option&gt;'.
     '&lt;option value=&quot;SD&quot;&gt;Sudan&lt;/option&gt;'.
     '&lt;option value=&quot;SR&quot;&gt;Suriname&lt;/option&gt;'.
     '&lt;option value=&quot;SJ&quot;&gt;Svalbard and Jan Mayen Islands&lt;/option&gt;'.
     '&lt;option value=&quot;SZ&quot;&gt;Swaziland&lt;/option&gt;'.
     '&lt;option value=&quot;SE&quot;&gt;Sweden&lt;/option&gt;'.
     '&lt;option value=&quot;CH&quot;&gt;Switzerland&lt;/option&gt;'.
     '&lt;option value=&quot;SY&quot;&gt;Syria&lt;/option&gt;'.
     '&lt;option value=&quot;TW&quot;&gt;Taiwan&lt;/option&gt;'.
     '&lt;option value=&quot;TJ&quot;&gt;Tajikistan&lt;/option&gt;'.
     '&lt;option value=&quot;TZ&quot;&gt;Tanzania&lt;/option&gt;'.
     '&lt;option value=&quot;TH&quot;&gt;Thailand&lt;/option&gt;'.
     '&lt;option value=&quot;TL&quot;&gt;Timor-Leste&lt;/option&gt;'.
     '&lt;option value=&quot;TG&quot;&gt;Togo&lt;/option&gt;'.
     '&lt;option value=&quot;TK&quot;&gt;Tokelau&lt;/option&gt;'.
     '&lt;option value=&quot;TO&quot;&gt;Tonga&lt;/option&gt;'.
     '&lt;option value=&quot;TT&quot;&gt;Trinidad and Tobago&lt;/option&gt;'.
     '&lt;option value=&quot;TN&quot;&gt;Tunisia&lt;/option&gt;'.
     '&lt;option value=&quot;TR&quot;&gt;Turkey&lt;/option&gt;'.
     '&lt;option value=&quot;TM&quot;&gt;Turkmenistan&lt;/option&gt;'.
     '&lt;option value=&quot;TC&quot;&gt;Turks and Caicos Islands&lt;/option&gt;'.
     '&lt;option value=&quot;TV&quot;&gt;Tuvalu&lt;/option&gt;'.
     '&lt;option value=&quot;UG&quot;&gt;Uganda&lt;/option&gt;'.
     '&lt;option value=&quot;UA&quot;&gt;Ukraine&lt;/option&gt;'.
     '&lt;option value=&quot;AE&quot;&gt;United Arab Emirates&lt;/option&gt;'.
     '&lt;option value=&quot;GB&quot;&gt;United Kingdom&lt;/option&gt;'.
     '&lt;option value=&quot;UM&quot;&gt;United States Minor Outlying Islands&lt;/option&gt;'.
     '&lt;option value=&quot;UY&quot;&gt;Uruguay&lt;/option&gt;'.
     '&lt;option value=&quot;UZ&quot;&gt;Uzbekistan&lt;/option&gt;'.
     '&lt;option value=&quot;VU&quot;&gt;Vanuatu&lt;/option&gt;'.
     '&lt;option value=&quot;VA&quot;&gt;Vatican City&lt;/option&gt;'.
     '&lt;option value=&quot;VE&quot;&gt;Venezuela&lt;/option&gt;'.
     '&lt;option value=&quot;VN&quot;&gt;Vietnam&lt;/option&gt;'.
     '&lt;option value=&quot;VG&quot;&gt;Virgin Islands ( British )&lt;/option&gt;'.
     '&lt;option value=&quot;VI&quot;&gt;Virgin Islands ( U.S. )&lt;/option&gt;'.
     '&lt;option value=&quot;WF&quot;&gt;Wallis and Futuna Islands&lt;/option&gt;'.
     '&lt;option value=&quot;EH&quot;&gt;Western Sahara&lt;/option&gt;'.
     '&lt;option value=&quot;YE&quot;&gt;Yemen&lt;/option&gt;'.
     '&lt;option value=&quot;ZM&quot;&gt;Zambia&lt;/option&gt;'.
     '&lt;option value=&quot;ZW&quot;&gt;Zimbabwe&lt;/option&gt;
&lt;/select&gt;
&lt;p&gt;State/Province:&lt;/p&gt;
&lt;p id=&quot;stateSelect&quot;&gt;&lt;select name=&quot;state&quot; id=&quot;state&quot; disabled=&quot;disabled&quot;&gt;&lt;option value=&quot;Other&quot;&gt;Select state...&lt;/option&gt;&lt;/select&gt;&lt;/p&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Country value:&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;p id=&quot;countryValue&quot;&gt;&lt;/p&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;State value:&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;p id=&quot;stateValue&quot;&gt;&lt;/p&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;</pre></div></div>

<p>After the user makes a country selection, the Javascript <strong>onChange</strong> event is fired and executes the <strong>printStateMenu</strong>  Javascript function and passes the value of the country that was chosen as an argument.  The table just displays the current values of the <strong>country</strong> and <strong>state</strong> select inputs.  Please note that the script source path on line 1 will have to edited depending on where you store the JavaScript function.  Here is the Javascript code for the <strong>printStateMenu</strong> function:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> printStateMenu<span style="color: #009900;">&#40;</span>country<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>     
	<span style="color: #003366; font-weight: bold;">var</span> stateSelect <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span>;
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>country <span style="color: #339933;">==</span> <span style="color: #3366CC;">'US'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		stateSelect <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;select name=&quot;state&quot; id=&quot;state&quot; onchange=&quot;document.getElementById(<span style="color: #000099; font-weight: bold;">\'</span>stateValue<span style="color: #000099; font-weight: bold;">\'</span>).innerHTML = this.value;&quot;&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;AK&quot;&gt;AK-Alaska&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;AL&quot;&gt;AL-Alabama&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;AR&quot;&gt;AR-Arkansas&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;AZ&quot;&gt;AZ-Arizona&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;CA&quot;&gt;CA-California&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;CO&quot;&gt;CO-Colorado&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;CT&quot;&gt;CT-Connecticut&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;DC&quot;&gt;DC-District of Columbia&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;DE&quot;&gt;DE-Delaware&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;FL&quot;&gt;FL-Florida&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;GA&quot;&gt;GA-Georgia&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;HI&quot;&gt;HI-Hawaii&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;IA&quot;&gt;IA-Iowa&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;ID&quot;&gt;ID-Idaho&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;IL&quot;&gt;IL-Illinois&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;IN&quot;&gt;IN-Indiana&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;KS&quot;&gt;KS-Kansas&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;KY&quot;&gt;KY-Kentucky&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;LA&quot;&gt;LA-Louisiana&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;MA&quot;&gt;MA-Massachusetts&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;MD&quot;&gt;MD-Maryland&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;ME&quot;&gt;ME-Maine&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;MI&quot;&gt;MI-Michigan&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;MN&quot;&gt;MN-Minnesota&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;MO&quot;&gt;MO-Missouri&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;MS&quot;&gt;MS-Mississippi&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;MT&quot;&gt;MT-Montana&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;NC&quot;&gt;NC-North Carolina&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;ND&quot;&gt;ND-North Dakota&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;NE&quot;&gt;NE-Nebraska&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;NH&quot;&gt;NH-New Hampshire&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;NJ&quot;&gt;NJ-New Jersey&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;NM&quot;&gt;NM-New Mexico&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;NV&quot;&gt;NV-Nevada&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;NY&quot;&gt;NY-New York&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;OH&quot;&gt;OH-Ohio&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;OK&quot;&gt;OK-Oklahoma&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;OR&quot;&gt;OR-Oregon&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;PA&quot;&gt;PA-Pennsylvania&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;RI&quot;&gt;RI-Rhode Island&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;SC&quot;&gt;SC-South Carolina&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;SD&quot;&gt;SD-South Dakota&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;TN&quot;&gt;TN-Tennessee&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;TX&quot;&gt;TX-Texas&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;UT&quot;&gt;UT-Utah&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;VA&quot;&gt;VA-Virginia&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;VT&quot;&gt;VT-Vermont&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;WA&quot;&gt;WA-Washington&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;WI&quot;&gt;WI-Wisconsin&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;WV&quot;&gt;WV-West Virginia&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;WY&quot;&gt;WY-Wyoming&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;/select&gt;'</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>country <span style="color: #339933;">==</span> <span style="color: #3366CC;">'CA'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		stateSelect <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;select name=&quot;state&quot; id=&quot;state&quot; onchange=&quot;document.getElementById(<span style="color: #000099; font-weight: bold;">\'</span>stateValue<span style="color: #000099; font-weight: bold;">\'</span>).innerHTML = this.value;&quot;&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;AB&quot;&gt;AB-Alberta&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;BC&quot;&gt;BC-British Columbia&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;MB&quot;&gt;MB-Manitoba&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;NB&quot;&gt;NB-New Brunswick&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;NL&quot;&gt;NL-Newfoundland and Labrador&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;NT&quot;&gt;NT-Northwest Territories&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;NS&quot;&gt;NS-Nova Scotia&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;NU&quot;&gt;NU-Nunavut&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;ON&quot;&gt;ON-Ontario&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;PE&quot;&gt;PE-Prince Edward Island&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;QC&quot;&gt;QC-Quebec&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;SK&quot;&gt;SK-Saskatchewan&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;YT&quot;&gt;YT-Yukon&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;/select&gt;'</span>;
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
		stateSelect <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;select name=&quot;state&quot; id=&quot;state&quot; disabled&gt;'</span>.
		<span style="color: #3366CC;">'&lt;option value=&quot;Other&quot;&gt;Select state...&lt;/option&gt;'</span>.
		<span style="color: #3366CC;">'&lt;/select&gt;'</span>;
	<span style="color: #009900;">&#125;</span>
	document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'stateSelect'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> stateSelect;
	document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'countryValue'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'country'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">value</span>;
	document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'stateValue'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'state'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">value</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This Javascript function is pretty simple.  It looks at the country value that was selected and creates the corresponding state/province menu.  Then it replaces the HTML code in the <strong>stateSelect</strong> DOM element with the new select menu.  The state/province select inputs for US and Canada also have an <strong>onChange</strong> event that displays the chosen value of the state input into the table.  The select menu for any other country will just return a disabled select menu and default to the <strong>Other</strong> value for the <strong>state</strong> input.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/02/09/dynamic-country-state-javascript-menu-example/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
