<?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</title>
	<atom:link href="http://www.randomsnippets.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.randomsnippets.com</link>
	<description>Random Snippets of Code for Web Developers</description>
	<lastBuildDate>Thu, 10 Jan 2013 21:56:10 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>How to hide or show content based on links or urls via JavaScript/jQuery</title>
		<link>http://www.randomsnippets.com/2011/10/09/how-to-hide-or-show-content-based-on-links-or-urls-via-javascript-jquery/</link>
		<comments>http://www.randomsnippets.com/2011/10/09/how-to-hide-or-show-content-based-on-links-or-urls-via-javascript-jquery/#comments</comments>
		<pubDate>Mon, 10 Oct 2011 01:56:33 +0000</pubDate>
		<dc:creator>Allen Liu</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[hide or show content]]></category>
		<category><![CDATA[hide or show content via url]]></category>

		<guid isPermaLink="false">http://www.randomsnippets.com/?p=600</guid>
		<description><![CDATA[I have been receiving many requests for a tutorial that shows how to hide or show content based on links or options that a user picks and reflected on a different page so here it is =) You can see &#8230; <a href="http://www.randomsnippets.com/2011/10/09/how-to-hide-or-show-content-based-on-links-or-urls-via-javascript-jquery/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>I have been receiving many requests for a tutorial that shows how to hide or show content based on links or options that a user picks and reflected on a different page so here it is =)</p>
<p>You can see the <a href="http://randomsnippets.com/demo/demo.html" title="How to hide or show content based on links or urls via JavaScript/jQuery">working demo here</a>.</p>
<p>Here is the plain HTML:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="html" style="font-family:monospace;">If you had to choose a drink, what would you choose:
&lt;ul&gt;
	&lt;li&gt;&lt;a href=&quot;/demo/demo-show-hide-based-on-url.html?option=coke&quot;&gt;Coke&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;/demo/demo-show-hide-based-on-url.html?option=bubble-tea&quot;&gt;Bubble Tea&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;/demo/demo-show-hide-based-on-url.html?option=milk&quot;&gt;Milk&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</pre></td></tr></table></div>

<p>It is a simple list of options for a user to select and the key is in the linking url.  There is an <code>option</code> parameter where we are passing over the user&#8217;s selected option.  The <code>option</code> values (coke, bubble-tea, and milk) will map to their respective div ids on the following page that we will dynamically hide or show.</p>
<p>Here is the JavaScript/jQuery code that executes on the linking page:</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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot; src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js&quot;&gt;&lt;/script&gt;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">var</span> option <span style="color: #339933;">=</span> <span style="color: #3366CC;">'coke'</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">var</span> url <span style="color: #339933;">=</span> window.<span style="color: #660066;">location</span>.<span style="color: #660066;">href</span><span style="color: #339933;">;</span>
	option <span style="color: #339933;">=</span> url.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/option=(.*)/</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	showDiv<span style="color: #009900;">&#40;</span>option<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">function</span> showDiv<span style="color: #009900;">&#40;</span>option<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.boxes'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#'</span> <span style="color: #339933;">+</span> option<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Line 1 imports the jQuery library from Google&#8217;s content distribution network (CDN). There are a couple of reasons why you should use CDNs including decreased latency and increased parallelism. If you are interested in reading the details, here is a <a href="http://www.ajaxlines.com/ajax/stuff/article/_reasons_why_you_should_let_google_host_jquery_for_you.php">nice post</a>.</p>
<p>Line 4 assigns the default div id that will be shown which in this case is <code>coke</code>.</p>
<p>Lines 5-6 parses the option value from the url information.</p>
<p>Line 7 calls the <code>showDiv()</code> function and passes the option value as an argument.</p>
<p>In line 10 of the <code>showDiv()</code> function, we hide all the divs in case there are any that are opened.  We do this using the jQuery class selector where we select all the elements with the <code>boxes</code> class.</p>
<p>Line 11 will use the id (e.g. #) jQuery selector using the option parameter that was passed over and show the element via the jQuery <code>.show()</code> method. </p>
<p>Here is the HTML code for the second page with our dynamic displayed content:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;div class=&quot;boxes&quot; id=&quot;coke&quot;&gt;Coke is awesome!&lt;/div&gt;
&lt;div class=&quot;boxes&quot; id=&quot;bubble-tea&quot;&gt;Bubble tea is da bomb!&lt;/div&gt;
&lt;div class=&quot;boxes&quot; id=&quot;milk&quot;&gt;Milk is healthy!&lt;/div&gt;
&lt;p&gt;
	I change my mind:
	&lt;ul&gt;
		&lt;li&gt;&lt;a href=&quot;javascript:showDiv('coke')&quot;&gt;Coke&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href=&quot;javascript:showDiv('bubble-tea')&quot;&gt;Bubble Tea&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href=&quot;javascript:showDiv('milk')&quot;&gt;Milk&lt;/a&gt;&lt;/li&gt;
	&lt;/ul&gt;
&lt;/p&gt;
&lt;a href=&quot;/demo/demo.html&quot;&gt;Back to main page&lt;/a&gt;</pre></td></tr></table></div>

<p>We begin with the divs, each with their respective ids (coke, bubble-tea, milk).  We then re-list our options on this page as well.  The links here do not link to another page though.  Instead, they call the <code>showDiv()</code> function and pass over the selected option.</p>
<p>If you found that my code was helpful in any way, shape, or form and would like to buy me a <strong>beer</strong>, please use the <strong>Donate</strong> button below =)  Cheers!  <strong>OR</strong>&#8230;you can sign up for a <a href="http://db.tt/aYkS0ZA">free Dropbox account using my referral link</a> =)  If you are not aware of what Dropbox is, it is the most awesomest online backup application there and so much more&#8230;all for <strong>free</strong>.</p>
<p><center></p>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick"><br />
<input type="hidden" name="hosted_button_id" value="WB5GF5WQ6YE5U"><br />
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"><br />
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1"><br />
</form>
<p></center></p>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2011/10/09/how-to-hide-or-show-content-based-on-links-or-urls-via-javascript-jquery/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>How to dynamically remove elements from the DOM via jQuery</title>
		<link>http://www.randomsnippets.com/2011/08/21/how-to-dynamically-remove-elements-from-the-dom-via-jquery/</link>
		<comments>http://www.randomsnippets.com/2011/08/21/how-to-dynamically-remove-elements-from-the-dom-via-jquery/#comments</comments>
		<pubDate>Mon, 22 Aug 2011 02:47:01 +0000</pubDate>
		<dc:creator>Allen Liu</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery remove]]></category>
		<category><![CDATA[remove div]]></category>
		<category><![CDATA[remove dom element]]></category>

		<guid isPermaLink="false">http://www.randomsnippets.com/?p=559</guid>
		<description><![CDATA[jQuery is an awesome JavaScript library that can do some very heavy lifting in very small amounts of code. Take the example below where we are simply removing a div element from the DOM. Demo Remove me by clicking the &#8230; <a href="http://www.randomsnippets.com/2011/08/21/how-to-dynamically-remove-elements-from-the-dom-via-jquery/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p><script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script><br />
<script type="text/javascript">
function removeDiv(divId) {
   $("#"+divId).remove();
}
function removeByClass(className) {
   $("."+className).remove();
}
</script></p>
<style type="text/css">
.blueBox {
   height: 100px; 
   width: 100px; 
   background-color: blue; 
   color: white;
   margin: 10px; 
   padding; 5px;
   float: left;
}
.redBox {
   height: 100px; 
   width: 100px; 
   background-color: red; 
   color: white;
   margin: 10px; 
   padding; 5px;
   float: left;
}
</style>
<p><a href="http://jquery.com/" title="jQuery" target="_blank">jQuery</a> is an awesome JavaScript library that can do some very heavy lifting in very small amounts of code.  Take the example below where we are simply removing a div element from the DOM.</p>
<fieldset>
<legend>Demo</legend>
<div id="removeMe" style="background-color: #333333; color: #FFFFFF; padding: 10px;">Remove me by clicking the button below.</div>
<p><button onclick="removeDiv('removeMe');">Remove</button><br />
</fieldset>
<p><span id="more-559"></span><br />
Here is the HTML code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;div id=&quot;removeMe&quot; style=&quot;background-color: #333333; color: #FFFFFF; padding: 10px;&quot;&gt;Remove me by clicking the button below.&lt;/div&gt;
&lt;button onclick=&quot;removeDiv('removeMe');&quot;&gt;Remove&lt;/button&gt;</pre></td></tr></table></div>

<p>Line 1 defines the div element with id=<code>removeMe</code> that we are planning to vanquish from the DOM.  There is some inline CSS thrown in for aesthetics and I apologize for that.  It just makes the div easier to visualize.  </p>
<p>Line 2 is our button where the <code>onclick</code> event calls the <code>removeDiv</code> JavaScript function.  In addition, the id of the div is passed over as an argument to the function.</p>
<p>Simple enough.  Now to the more exciting stuff.</p>
<p>Here is the JavaScript code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> removeDiv<span style="color: #009900;">&#40;</span>divId<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#&quot;</span><span style="color: #339933;">+</span>divId<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>That&#8217;s it and that is why I <3 jQuery =)  </p>
<p>Let's break down line 2 because there is actually quite a bit that is going on here with 22 characters.  </p>
<p><code>$(&#8220;#&#8221;+divId)</code> is jQuery syntax which uses the <a href="http://api.jquery.com/id-selector/" title="jQuery ID selector" target="_blank">ID selector</a> to help us select our div.  The divId parameter is appended to the ID selector so that the function is dynamic and can handle any div or element in the DOM.  When a user clicks the button, the <code>removeMe</code> argument is passed over so the selector becomes <code>$("#removeMe")</code> after the variable is interpolated. </p>
<p>Ok, so now we have selected our div element, we want to remove it from the DOM.  This is where the <code>.remove()</code> jQuery function comes in handy.  Simply append this function to the selected element and the rest is history.</p>
<p>Oh but the fun does not stop here.  With the use of the all-mighty jQuery selector, we can easily remove multiple elements at once.  Looky here.</p>
<fieldset>
<legend>Demo</legend>
<div class="blueBox">Blue box #1</div>
<div class="blueBox">Blue box #2</div>
<div class="blueBox">Blue box #3</div>
<div class="redBox">Red box #1</div>
<div class="redBox">Red box #2</div>
<div class="redBox">Red box #3</div>
<div style="clear: both;"></div>
<p><button onclick="removeByClass('blueBox');">Remove just the blue boxes</button><br />
<button onclick="removeByClass('redBox');">Remove just the red boxes</button><br />
</fieldset>
<p>Here is the HTML and CSS code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;style type=&quot;text/css&quot;&gt;
.blueBox {
   height: 100px; 
   width: 100px; 
   background-color: blue; 
   color: white;
   margin: 10px; 
   padding; 5px;
   float: left;
}
.redBox {
   height: 100px; 
   width: 100px; 
   background-color: red; 
   color: white;
   margin: 10px; 
   padding; 5px;
   float: left;
}
&lt;/style&gt;
&lt;div class=&quot;blueBox&quot;&gt;Blue box #1&lt;/div&gt;
&lt;div class=&quot;blueBox&quot;&gt;Blue box #2&lt;/div&gt;
&lt;div class=&quot;blueBox&quot;&gt;Blue box #3&lt;/div&gt;
&lt;div class=&quot;redBox&quot;&gt;Red box #1&lt;/div&gt;
&lt;div class=&quot;redBox&quot;&gt;Red box #2&lt;/div&gt;
&lt;div class=&quot;redBox&quot;&gt;Red box #3&lt;/div&gt;
&lt;div style=&quot;clear: both;&quot;&gt;&lt;/div&gt;
&lt;button onclick=&quot;removeByClass('blueBox');&quot;&gt;Remove just the blue boxes&lt;/button&gt;
&lt;button onclick=&quot;removeByClass('redBox');&quot;&gt;Remove just the red boxes&lt;/button&gt;</pre></td></tr></table></div>

<p>The CSS is nicely separated from our code which is best practice (and easier to maintain).  The HTML code defines 3 blue boxes with the <code>class</code> attribute <code>blueBox</code> and 3 red boxes with the <code>class</code> attribute <code>redBox</code>.  We also have one button to remove the blue boxes by calling the <code>removeByClass</code> function and passing over the <code>blueBox</code> class name.  Then we have another button that calls the same function but passes the <code>redBox</code> class name.</p>
<p>Now for the JavaScript code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> removeByClass<span style="color: #009900;">&#40;</span>className<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.&quot;</span><span style="color: #339933;">+</span>className<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>That's right.  It's almost identical as our original function except we now use <code>.</code> instead of <code>#</code> which is the <a href="http://api.jquery.com/class-selector/" title="jQuery class selector" target="_blank">jQuery class selector</a>.  Did I mention how much I love jQuery?</p>
<p>This time, we simply plug in the class name to our class selector which selects ALL elements with that particular class name.  Then we append the <code>.remove()</code> function as before and all elements that are selected will be removed.  Pretty sweet eh? =)</p>
<p>If you found that my code was helpful in any way, shape, or form and would like to buy me a <strong>beer</strong>, please use the <strong>Donate</strong> button below =)  Cheers!<br />
<center></p>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick"><br />
<input type="hidden" name="hosted_button_id" value="WB5GF5WQ6YE5U"><br />
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"><br />
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1"><br />
</form>
<p></center></p>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2011/08/21/how-to-dynamically-remove-elements-from-the-dom-via-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript Date Ranges/Calculations with Datejs</title>
		<link>http://www.randomsnippets.com/2011/07/30/javascript-date-ranges-calculations-with-datejs/</link>
		<comments>http://www.randomsnippets.com/2011/07/30/javascript-date-ranges-calculations-with-datejs/#comments</comments>
		<pubDate>Sat, 30 Jul 2011 09:18:36 +0000</pubDate>
		<dc:creator>Allen Liu</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[datejs]]></category>
		<category><![CDATA[javascript date range]]></category>
		<category><![CDATA[javascript dates]]></category>

		<guid isPermaLink="false">http://www.randomsnippets.com/?p=424</guid>
		<description><![CDATA[If there is anything I hate more than the smell of durian, it is having to deal with date range calculations in JavaScript. Luckily though, we have the Datejs JavaScript Date Library to prevent us from having to pull our &#8230; <a href="http://www.randomsnippets.com/2011/07/30/javascript-date-ranges-calculations-with-datejs/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p><script type="text/javascript" src="/wp-includes/js/date.js"></script><br />
<script type="text/javascript" src="/wp-includes/js/dateRange.js"></script><br />
If there is anything I hate more than the <a href="http://en.wikipedia.org/wiki/Durian">smell of durian</a>, it is having to deal with date range calculations in JavaScript. Luckily though, we have the <a href="http://www.datejs.com/">Datejs JavaScript Date Library</a> to prevent us from having to pull our hair out.</p>
<h3>Mission</h3>
<p>I was recently faced with having to implement a date range drop-down menu very similar to of the one that is beautifully implemented in Google Adwords:</p>
<p><a href="http://www.randomsnippets.com/wp-content/uploads/2011/07/google-adwords-date-range-selector.png"><img src="http://www.randomsnippets.com/wp-content/uploads/2011/07/google-adwords-date-range-selector.png" alt="Google Adwords Date Range Pull-Down Menu" title="Google Adwords Date Range Pull-Down Menu" width="292" height="402" class="alignnone size-full wp-image-426" /></a></p>
<div style="clear: both;"></div>
<p>The select input element had to return 3 pieces of information: The <strong>name of the option</strong>, <strong>start date</strong>, and <strong>end date</strong> of the selected option.</p>
<p>Here are the specific options that I had to implement for the date ranges:</p>
<ul>
<li>This month to date</li>
<li>This quarter to date</li>
<li>Last 7 days</li>
<li>Last 30 days</li>
<li>Last 365 days</li>
<li>Last week (Mon to Sun)</li>
<li>Last full month</li>
<li>Last full quarter</li>
<li>Last full year</li>
</ul>
<h3>The Plan</h3>
<p><span id="more-424"></span><br />
The tricky thing about select input elements is that they only return one value but this mission required me to return the option name, a start date, and an end date.  The solution I chose to go with was a <a href="http://www.json.org/">JSON</a> string with the following name/value pairs for each option:</p>
<p><code><span style="color:green">'[display name of the option]'</span>:<span style="color:red">'{start:[start date],end:[end date]}'</span></code></p>
<p>It looks messy but it really is one name/value pair depicted in different colors.  The value itself, though, is a JSON array that contains 2 name/value pairs: The start and end dates.</p>
<p>The actual value string for one of the options would look something like this:</p>
<p><code>'Last week (Mon to Sun)':'{"start":"07/18/2011","end":"07/24/2011"}'</code></p>
<p>Once all of the select values are calculated using Datejs, the plan is to populate a select input element with all the options via jQuery.  This allows for more of a separation between the HTML and the JavaScript code which is easier to maintain and a bit more organized.</p>
<h3>The Code</h3>
<p>Let&#8217;s start easy and begin with the HTML code for the select input:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;select id=&quot;dateRange&quot; name=&quot;dateRange&quot;&gt;
&lt;/select&gt;</pre></td></tr></table></div>

<p>That was pretty painless.  We&#8217;ve prepared the select input element and gave it an id and a name but we have yet to populate it with the options.  This will be taken care of later with the help of JavaScript.</p>
<p>The next part of the plan is to do the actual date range calculations.  This was done relatively easily with the help of Datejs.  Here is the JavaScript code for that:</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
36
37
38
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">// Import Datejs JavaScript Library
&lt;script type=&quot;text/javascript&quot; src=&quot;[PATH_TO_YOUR_DATEJS_FILE]/date.js&quot;&gt;&lt;/script&gt;
&nbsp;
// Calculate date ranges for date pull down menu
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #000066; font-weight: bold;">var</span> today <span style="color: #339933;">=</span> <span style="">Date</span>.<span style="color: #660066;">parse</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'today'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'yyyy-MM-dd'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> firstDayOfMonth <span style="color: #339933;">=</span> <span style="">Date</span>.<span style="color: #660066;">today</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">moveToFirstDayOfMonth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'yyyy-MM-dd'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> month <span style="color: #339933;">=</span> <span style="">Date</span>.<span style="color: #660066;">parse</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'today'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'MM'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> year <span style="color: #339933;">=</span> <span style="">Date</span>.<span style="color: #660066;">parse</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'today'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'yyyy'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> quarterMonth <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="">Math</span>.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>month<span style="color: #339933;">/</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> quarter <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="">Math</span>.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>month<span style="color: #339933;">/</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> lastQuarter <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>quarter <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> quarter <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span> <span style="color: #339933;">:</span> lastQuarter <span style="color: #339933;">=</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> quarterStartDate <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>quarterMonth <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> year<span style="color: #339933;">+</span><span style="color: #3366CC;">'-0'</span><span style="color: #339933;">+</span>quarterMonth<span style="color: #339933;">+</span><span style="color: #3366CC;">'-01'</span> <span style="color: #339933;">:</span> year<span style="color: #339933;">+</span><span style="color: #3366CC;">'-'</span><span style="color: #339933;">+</span>quarterMonth<span style="color: #339933;">+</span><span style="color: #3366CC;">'-01'</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> lastSevenDaysStart <span style="color: #339933;">=</span> <span style="">Date</span>.<span style="color: #660066;">today</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">6</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">days</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'yyyy-MM-dd'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> lastThirtyDaysStart <span style="color: #339933;">=</span> <span style="">Date</span>.<span style="color: #660066;">today</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">29</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">days</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'yyyy-MM-dd'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> lastYearDayStart <span style="color: #339933;">=</span> <span style="">Date</span>.<span style="color: #660066;">today</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">364</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">days</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'yyyy-MM-dd'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> lastFullWeekStart <span style="color: #339933;">=</span> <span style="">Date</span>.<span style="color: #660066;">today</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">7</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">days</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">last</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">monday</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'yyyy-MM-dd'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> lastFullWeekEnd <span style="color: #339933;">=</span> <span style="">Date</span>.<span style="color: #660066;">today</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">7</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">days</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">last</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">monday</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">6</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">days</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'yyyy-MM-dd'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> lastFullMonthStart <span style="color: #339933;">=</span> <span style="">Date</span>.<span style="color: #660066;">today</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">months</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">moveToFirstDayOfMonth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'yyyy-MM-dd'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> lastFullMonthEnd <span style="color: #339933;">=</span> <span style="">Date</span>.<span style="color: #660066;">today</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">months</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">moveToLastDayOfMonth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'yyyy-MM-dd'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> lastFullQuarterStart <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>lastQuarter<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: #CC0000;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> year<span style="color: #339933;">+</span><span style="color: #3366CC;">'-0'</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>lastQuarter<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: #CC0000;">3</span><span style="color: #009900;">&#41;</span><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;">'-01'</span> <span style="color: #339933;">:</span> year<span style="color: #339933;">+</span><span style="color: #3366CC;">'-'</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>lastQuarter<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: #CC0000;">3</span><span style="color: #009900;">&#41;</span><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;">'-01'</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> lastFullQuarterEnd <span style="color: #339933;">=</span> <span style="">Date</span>.<span style="color: #660066;">parse</span><span style="color: #009900;">&#40;</span>lastFullQuarterStart<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">months</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">moveToLastDayOfMonth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'yyyy-MM-dd'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> lastFullYearStart <span style="color: #339933;">=</span> <span style="">Date</span>.<span style="color: #660066;">today</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">years</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'yyyy'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'-01-01'</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> lastFullYearEnd <span style="color: #339933;">=</span> <span style="">Date</span>.<span style="color: #660066;">today</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">years</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'yyyy'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'-12-31'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Prepare JavaScript array with the JSON </span>
<span style="color: #000066; font-weight: bold;">var</span> selectOptions <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">'This month to date'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'{&quot;start&quot;:&quot;'</span><span style="color: #339933;">+</span>firstDayOfMonth<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;,&quot;end&quot;:&quot;'</span><span style="color: #339933;">+</span>today<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;}'</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">'This quarter to date'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'{&quot;start&quot;:&quot;'</span><span style="color: #339933;">+</span>quarterStartDate<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;,&quot;end&quot;:&quot;'</span><span style="color: #339933;">+</span>today<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;}'</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">'Last 7 days'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'{&quot;start&quot;:&quot;'</span><span style="color: #339933;">+</span>lastSevenDaysStart<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;,&quot;end&quot;:&quot;'</span><span style="color: #339933;">+</span>today<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;}'</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">'Last 30 days'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'{&quot;start&quot;:&quot;'</span><span style="color: #339933;">+</span>lastThirtyDaysStart<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;,&quot;end&quot;:&quot;'</span><span style="color: #339933;">+</span>today<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;}'</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">'Last 365 days'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'{&quot;start&quot;:&quot;'</span><span style="color: #339933;">+</span>lastYearDayStart<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;,&quot;end&quot;:&quot;'</span><span style="color: #339933;">+</span>today<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;}'</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">'Last week (Mon to Sun)'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'{&quot;start&quot;:&quot;'</span><span style="color: #339933;">+</span>lastFullWeekStart<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;,&quot;end&quot;:&quot;'</span><span style="color: #339933;">+</span>lastFullWeekEnd<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;}'</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">'Last full month'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'{&quot;start&quot;:&quot;'</span><span style="color: #339933;">+</span>lastFullMonthStart<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;,&quot;end&quot;:&quot;'</span><span style="color: #339933;">+</span>lastFullMonthEnd<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;}'</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">'Last full quarter'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'{&quot;start&quot;:&quot;'</span><span style="color: #339933;">+</span>lastFullQuarterStart<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;,&quot;end&quot;:&quot;'</span><span style="color: #339933;">+</span>lastFullQuarterEnd<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;}'</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">'Last full year'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'{&quot;start&quot;:&quot;'</span><span style="color: #339933;">+</span>lastFullYearStart<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;,&quot;end&quot;:&quot;'</span><span style="color: #339933;">+</span>lastFullYearEnd<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;}'</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Line 2 imports the Datejs library.  You will have to download it and install via these <a href="http://www.datejs.com/2007/11/27/getting-started-with-datejs/">instructions</a>.  Lines 5-23 calculates each of the date elements that I will need to piece together the final product.  All the variables are pretty much self explanatory so I will not do the play-by-play for each line.</p>
<p>The excitement happens in lines 26-36 where everything is pieced together to form our JSON strings and put into an array called <strong>selectOptions</strong>.</p>
<p>Ok, now we have our empty select input and a JavaScript array with all our options.  Next, we enlist the help of jQuery to help populate the select input element.</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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">// Load jQuery library hosted by Google - Note: If you already have jQuery imported DELETE the next line.
&lt;script src=&quot;//ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
&nbsp;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>selectOptions<span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>key<span style="color: #339933;">,</span> value<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>key <span style="color: #339933;">==</span> <span style="color: #3366CC;">'Last 30 days'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#date_range'</span><span style="color: #009900;">&#41;</span>.
                <span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;option&gt;&lt;/option&gt;&quot;</span><span style="color: #009900;">&#41;</span>.
                <span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;value&quot;</span><span style="color: #339933;">,</span> value<span style="color: #009900;">&#41;</span>.
                <span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;selected&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;selected&quot;</span><span style="color: #009900;">&#41;</span>.
                <span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>           
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#date_range'</span><span style="color: #009900;">&#41;</span>.
                <span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;option&gt;&lt;/option&gt;&quot;</span><span style="color: #009900;">&#41;</span>.
                <span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;value&quot;</span><span style="color: #339933;">,</span> value<span style="color: #009900;">&#41;</span>.
                <span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Line 2 imports the jQuery library which Google so nicely hosted for us =)  Please remove or comment out this line if you already have jQuery working on your site.  Line 5 contains the jQuery ready() function which is executed when the DOM is fully loaded.  This is important for us because we need to have the select input element in place first before we can load the options.  That makes sense.  You need to watch <strong>The Empire Strikes Back</strong> before you watch <strong>Return of the Jedi</strong>.</p>
<p>Line 6 is the jQuery foreach function which will loop through each of our options in the <strong>selectOptions</strong> array by using the key => value pair (shown earlier in the <span style="color:green">Christmas</span> <span style="color:red">colors</span>).  Lines 7-13 tests to see if the current key is equal to &#8220;Last 30 days&#8221; and if so, it will make it the default selected option.  It does so by:</p>
<ol>
<li>Dynamically creating the <strong>option</strong> element and appending it to our select input that is accessed by the <a href="http://api.jquery.com/category/selectors/">jQuery selector</a> using the &#8220;date_range&#8221; id. [lines 8-9]</li>
<li>Dynamically setting the <strong>value</strong> attribute of the option to the value from the name/value pair (this contains our start and end dates). [line 10]</li>
<li>Dynamically setting the <strong>selected</strong> attribute to true which will make this option the default selected option. [line 11]</li>
<li>Dynamically setting the <strong>displayed text</strong> of the option to the key which holds the display name for the option (e.g. &#8220;Last 30 days&#8221;). [line 12]</li>
</ol>
<p>Lines 14-19 processes all the other elements in the same manner except that it does not set the <strong>selected</strong> attribute to <strong>true</strong>.</p>
<p>Here is the final product in all its glory:</p>
<select id="date_range" name="date_range"></select>
<p>Note: I noticed that the default selected option is NOT working correctly.  I know this codes works in the original implementation and I have tried debugging it here but it is 2:12AM and I need sleep!  I will come back to this at a later time&#8230;I promise.</p>
<p>To make this easier for you to implement, here is all the JavaScript code put together:</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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">// Load jQuery library hosted by Google - Note: If you already have jQuery imported DELETE the next line.
&lt;script src=&quot;//ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
&nbsp;
// Import Datejs JavaScript Library
&lt;script type=&quot;text/javascript&quot; src=&quot;[PATH_TO_YOUR_DATEJS_FILE]/date.js&quot;&gt;&lt;/script&gt;
&nbsp;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>selectOptions<span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>key<span style="color: #339933;">,</span> value<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>key <span style="color: #339933;">==</span> <span style="color: #3366CC;">'Last 30 days'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#date_range'</span><span style="color: #009900;">&#41;</span>.
                <span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;option&gt;&lt;/option&gt;&quot;</span><span style="color: #009900;">&#41;</span>.
                <span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;value&quot;</span><span style="color: #339933;">,</span> value<span style="color: #009900;">&#41;</span>.
                <span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;selected&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;selected&quot;</span><span style="color: #009900;">&#41;</span>.
                <span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>           
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#date_range'</span><span style="color: #009900;">&#41;</span>.
                <span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;option&gt;&lt;/option&gt;&quot;</span><span style="color: #009900;">&#41;</span>.
                <span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;value&quot;</span><span style="color: #339933;">,</span> value<span style="color: #009900;">&#41;</span>.
                <span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Calculate date ranges for date pull down menu</span>
<span style="color: #000066; font-weight: bold;">var</span> today <span style="color: #339933;">=</span> <span style="">Date</span>.<span style="color: #660066;">parse</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'today'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'yyyy-MM-dd'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> firstDayOfMonth <span style="color: #339933;">=</span> <span style="">Date</span>.<span style="color: #660066;">today</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">moveToFirstDayOfMonth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'yyyy-MM-dd'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> month <span style="color: #339933;">=</span> <span style="">Date</span>.<span style="color: #660066;">parse</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'today'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'MM'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> year <span style="color: #339933;">=</span> <span style="">Date</span>.<span style="color: #660066;">parse</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'today'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'yyyy'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> quarterMonth <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="">Math</span>.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>month<span style="color: #339933;">/</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> quarter <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="">Math</span>.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>month<span style="color: #339933;">/</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> lastQuarter <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>quarter <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> quarter <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span> <span style="color: #339933;">:</span> lastQuarter <span style="color: #339933;">=</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> quarterStartDate <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>quarterMonth <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> year<span style="color: #339933;">+</span><span style="color: #3366CC;">'-0'</span><span style="color: #339933;">+</span>quarterMonth<span style="color: #339933;">+</span><span style="color: #3366CC;">'-01'</span> <span style="color: #339933;">:</span> year<span style="color: #339933;">+</span><span style="color: #3366CC;">'-'</span><span style="color: #339933;">+</span>quarterMonth<span style="color: #339933;">+</span><span style="color: #3366CC;">'-01'</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> lastSevenDaysStart <span style="color: #339933;">=</span> <span style="">Date</span>.<span style="color: #660066;">today</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">6</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">days</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'yyyy-MM-dd'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> lastThirtyDaysStart <span style="color: #339933;">=</span> <span style="">Date</span>.<span style="color: #660066;">today</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">29</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">days</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'yyyy-MM-dd'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> lastYearDayStart <span style="color: #339933;">=</span> <span style="">Date</span>.<span style="color: #660066;">today</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">364</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">days</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'yyyy-MM-dd'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> lastFullWeekStart <span style="color: #339933;">=</span> <span style="">Date</span>.<span style="color: #660066;">today</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">7</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">days</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">last</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">monday</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'yyyy-MM-dd'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> lastFullWeekEnd <span style="color: #339933;">=</span> <span style="">Date</span>.<span style="color: #660066;">today</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">7</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">days</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">last</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">monday</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">6</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">days</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'yyyy-MM-dd'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> lastFullMonthStart <span style="color: #339933;">=</span> <span style="">Date</span>.<span style="color: #660066;">today</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">months</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">moveToFirstDayOfMonth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'yyyy-MM-dd'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> lastFullMonthEnd <span style="color: #339933;">=</span> <span style="">Date</span>.<span style="color: #660066;">today</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">months</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">moveToLastDayOfMonth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'yyyy-MM-dd'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> lastFullQuarterStart <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>lastQuarter<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: #CC0000;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> year<span style="color: #339933;">+</span><span style="color: #3366CC;">'-0'</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>lastQuarter<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: #CC0000;">3</span><span style="color: #009900;">&#41;</span><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;">'-01'</span> <span style="color: #339933;">:</span> year<span style="color: #339933;">+</span><span style="color: #3366CC;">'-'</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>lastQuarter<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: #CC0000;">3</span><span style="color: #009900;">&#41;</span><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;">'-01'</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> lastFullQuarterEnd <span style="color: #339933;">=</span> <span style="">Date</span>.<span style="color: #660066;">parse</span><span style="color: #009900;">&#40;</span>lastFullQuarterStart<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">months</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">moveToLastDayOfMonth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'yyyy-MM-dd'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> lastFullYearStart <span style="color: #339933;">=</span> <span style="">Date</span>.<span style="color: #660066;">today</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">years</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'yyyy'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'-01-01'</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> lastFullYearEnd <span style="color: #339933;">=</span> <span style="">Date</span>.<span style="color: #660066;">today</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">years</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'yyyy'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'-12-31'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Prepare JavaScript array with the JSON </span>
<span style="color: #000066; font-weight: bold;">var</span> selectOptions <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">'This month to date'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'{&quot;start&quot;:&quot;'</span><span style="color: #339933;">+</span>firstDayOfMonth<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;,&quot;end&quot;:&quot;'</span><span style="color: #339933;">+</span>today<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;}'</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">'This quarter to date'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'{&quot;start&quot;:&quot;'</span><span style="color: #339933;">+</span>quarterStartDate<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;,&quot;end&quot;:&quot;'</span><span style="color: #339933;">+</span>today<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;}'</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">'Last 7 days'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'{&quot;start&quot;:&quot;'</span><span style="color: #339933;">+</span>lastSevenDaysStart<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;,&quot;end&quot;:&quot;'</span><span style="color: #339933;">+</span>today<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;}'</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">'Last 30 days'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'{&quot;start&quot;:&quot;'</span><span style="color: #339933;">+</span>lastThirtyDaysStart<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;,&quot;end&quot;:&quot;'</span><span style="color: #339933;">+</span>today<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;}'</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">'Last 365 days'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'{&quot;start&quot;:&quot;'</span><span style="color: #339933;">+</span>lastYearDayStart<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;,&quot;end&quot;:&quot;'</span><span style="color: #339933;">+</span>today<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;}'</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">'Last week (Mon to Sun)'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'{&quot;start&quot;:&quot;'</span><span style="color: #339933;">+</span>lastFullWeekStart<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;,&quot;end&quot;:&quot;'</span><span style="color: #339933;">+</span>lastFullWeekEnd<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;}'</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">'Last full month'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'{&quot;start&quot;:&quot;'</span><span style="color: #339933;">+</span>lastFullMonthStart<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;,&quot;end&quot;:&quot;'</span><span style="color: #339933;">+</span>lastFullMonthEnd<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;}'</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">'Last full quarter'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'{&quot;start&quot;:&quot;'</span><span style="color: #339933;">+</span>lastFullQuarterStart<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;,&quot;end&quot;:&quot;'</span><span style="color: #339933;">+</span>lastFullQuarterEnd<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;}'</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">'Last full year'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'{&quot;start&quot;:&quot;'</span><span style="color: #339933;">+</span>lastFullYearStart<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;,&quot;end&quot;:&quot;'</span><span style="color: #339933;">+</span>lastFullYearEnd<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;}'</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>And there you have it!  I hope this helps someone out there in the ether =)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2011/07/30/javascript-date-ranges-calculations-with-datejs/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to hide, show, or toggle your div with jQuery</title>
		<link>http://www.randomsnippets.com/2011/04/10/how-to-hide-show-or-toggle-your-div-with-jquery/</link>
		<comments>http://www.randomsnippets.com/2011/04/10/how-to-hide-show-or-toggle-your-div-with-jquery/#comments</comments>
		<pubDate>Mon, 11 Apr 2011 04:58:21 +0000</pubDate>
		<dc:creator>Allen Liu</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[Hide]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[show]]></category>
		<category><![CDATA[toggle]]></category>

		<guid isPermaLink="false">http://www.randomsnippets.com/?p=336</guid>
		<description><![CDATA[Hiding and showing content is MUCH easier with the magic of jQuery. Here is a simple demo of the jQuery .toggle() function. Demo Toggle Button The content in this div will hide and show (toggle) when the toggle is pressed. &#8230; <a href="http://www.randomsnippets.com/2011/04/10/how-to-hide-show-or-toggle-your-div-with-jquery/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p><script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script><br />
<script type="text/javascript">
function toggleDiv(divId) {
   $("#"+divId).toggle();
}
function showonlyone(thechosenone) {
     $('.newboxes').each(function(index) {
          if ($(this).attr("id") == thechosenone) {
               $(this).show(200);
          }
          else {
               $(this).hide(600);
          }
     });
}
function slideonlyone(thechosenone) {
     $('.newboxes2').each(function(index) {
          if ($(this).attr("id") == thechosenone) {
               $(this).slideDown(200);
          }
          else {
               $(this).slideUp(600);
          }
     });
}
function toggleAndChangeText() {
     $('#divToToggle').toggle();
     if ($('#divToToggle').css('display') == 'none') {
          $('#aTag').html('Collapsed text mode &#9658');
     }
     else {
          $('#aTag').html('Expanded text mode &#9660');
     }
}
$(document).ready(function() {
     $('#clickme').click(function() {
          $('#me').animate({
               height: 'toggle'
               }, 2000
          );
     });
});
function toggleByClass(className) {
     $("."+className).toggle();
}
</script></p>
<style type="text/css">
.evenNumber, .oddNumber {
     width: 100px;
     height: 100px;
     margin: 10px;
     padding: 10px;
     float: left;     
     background-color: #333333;
     color: #FFFFFF;
}
</style>
<p>Hiding and showing content is MUCH easier with the magic of <a href="http://jquery.com/">jQuery</a>.  Here is a simple demo of the jQuery <code>.toggle()</code> function.</p>
<fieldset>
<legend>Demo</legend>
<p><a href="javascript:toggleDiv('myContent');" style="background-color: #ccc; padding: 5px 10px;">Toggle Button</a></p>
<div id="myContent" style="background-color: #aaa; padding: 5px 10px;">
The content in this div will hide and show (toggle) when the toggle is pressed.
</div>
</fieldset>
<p>Here is the JavaScript needed to run this demo:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot; src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js&quot;&gt;&lt;/script&gt;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #000066; font-weight: bold;">function</span> toggleDiv<span style="color: #009900;">&#40;</span>divId<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#&quot;</span><span style="color: #339933;">+</span>divId<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toggle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p><span id="more-336"></span><br />
Line 1 simply loads the minimized jQuery v1.4.4 library.  Once that is done, the rest is cake =)  Line 3 defines the <code>toggleDiv</code> JavaScript function which takes one parameter: the id of the div to be toggled.  Line 4 is where all the action takes place.  The <code>$("#"+divId)</code> part is the powerful jQuery selector which selects the div that was passed to the function.  I cannot say how easy<a href="http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery"> jQuery makes it to select elements</a> whether it is by id, class, or even by element types.  Once our div is selected, we simply append jQuery&#8217;s <code>toggle()</code> function which automatically detects whether or not the content hidden and toggles it.  On the HTML code level, the function toggles the <code>display</code> attribute between <code>none</code> and <code>block</code>.</p>
<p>Here is the raw HTML code for the demo:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;a href=&quot;javascript:toggleDiv('myContent');&quot; style=&quot;background-color: #ccc; padding: 5px 10px;&quot;&gt;Toggle Button&lt;/a&gt;
&lt;div id=&quot;myContent&quot; style=&quot;background-color: #aaa; padding: 5px 10px;&quot;&gt;
The content in this div will hide and show (toggle) when the toggle is pressed. 
&lt;/div&gt;</pre></td></tr></table></div>

<p>The <code>a tag</code> is styled to look like a button and the <code>onclick</code> event uses our <code>toggleDiv</code> which passes the &#8216;myContent&#8217; div ID as the argument.  The rest of the HTML basically codes for our &#8216;myContent&#8217; div whose content will be toggled by a click of the <code>a tag</code>.</p>
<p>That is it!  There is no more need to reinvent the wheel my friends.  </p>
<hr />
<p>Here is a new demo in response to a request where only one div is displayed at any one time.</p>
<fieldset>
<legend>Demo</legend>
<table>
<tr>
<td>
<div style="border: 1px solid blue; background-color: #99CCFF; padding: 5px; width: 150px;">
            <a id="myHeader1" href="javascript:showonlyone('newboxes1');" >show this one only</a>
         </div>
<div class="newboxes" id="newboxes1" style="border: 1px solid black; background-color: #CCCCCC; display: block;padding: 5px; width: 150px;">Div #1</div>
</td>
<td>
<div style="border: 1px solid blue; background-color: #99CCFF; padding: 5px; width: 150px;">
            <a id="myHeader2" href="javascript:showonlyone('newboxes2');" >show this one only</a>
         </div>
<div class="newboxes" id="newboxes2" style="border: 1px solid black; background-color: #CCCCCC; display: none;padding: 5px; width: 150px;">Div #2</div>
</td>
<td>
<div style="border: 1px solid blue; background-color: #99CCFF; padding: 5px; width: 150px;">
            <a id="myHeader3" href="javascript:showonlyone('newboxes3');" >show this one only</a>
         </div>
<div class="newboxes" id="newboxes3" style="border: 1px solid black; background-color: #CCCCCC; display: none;padding: 5px; width: 150px;">Div #3</div>
</td>
</tr>
</table>
</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
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;table&gt;
   &lt;tr&gt;
      &lt;td&gt;
         &lt;div style=&quot;border: 1px solid blue; background-color: #99CCFF; padding: 5px; width: 150px;&quot;&gt;
            &lt;a id=&quot;myHeader1&quot; href=&quot;javascript:showonlyone('newboxes1');&quot; &gt;show this one only&lt;/a&gt;
         &lt;/div&gt;
         &lt;div class=&quot;newboxes&quot; id=&quot;newboxes1&quot; style=&quot;border: 1px solid black; background-color: #CCCCCC; display: block;padding: 5px; width: 150px;&quot;&gt;Div #1&lt;/div&gt;
      &lt;/td&gt;
      &lt;td&gt;
         &lt;div style=&quot;border: 1px solid blue; background-color: #99CCFF; padding: 5px; width: 150px;&quot;&gt;
            &lt;a id=&quot;myHeader2&quot; href=&quot;javascript:showonlyone('newboxes2');&quot; &gt;show this one only&lt;/a&gt;
         &lt;/div&gt;
         &lt;div class=&quot;newboxes&quot; id=&quot;newboxes2&quot; style=&quot;border: 1px solid black; background-color: #CCCCCC; display: none;padding: 5px; width: 150px;&quot;&gt;Div #2&lt;/div&gt;
      &lt;/td&gt;
      &lt;td&gt;
         &lt;div style=&quot;border: 1px solid blue; background-color: #99CCFF; padding: 5px; width: 150px;&quot;&gt;
            &lt;a id=&quot;myHeader3&quot; href=&quot;javascript:showonlyone('newboxes3');&quot; &gt;show this one only&lt;/a&gt;
         &lt;/div&gt;
         &lt;div class=&quot;newboxes&quot; id=&quot;newboxes3&quot; style=&quot;border: 1px solid black; background-color: #CCCCCC; display: none;padding: 5px; width: 150px;&quot;&gt;Div #3&lt;/div&gt;
      &lt;/td&gt;
   &lt;/tr&gt;
&lt;/table&gt;</pre></td></tr></table></div>

<p>The HTML code contains 3 divs to start off with: 2 hidden and 1 displayed.  Each link will launch the <b>showonlyone</b> JavaScript function and pass over the corresponding div ID.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> showonlyone<span style="color: #009900;">&#40;</span>thechosenone<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.newboxes'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>index<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>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;id&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> thechosenone<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
               $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #009900;">&#125;</span>
          <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
               $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">600</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #009900;">&#125;</span>
     <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Line 2 contains some very cool uses of jQuery.  Here we are looping through all divs with the <code>class = "newboxes"</code> which represents all 3 divs.  </p>
<p>Line 3 checks to see if the div id in the current loop is equal to <strong>thechosenone</strong> which is the argument that was passed to the function.  </p>
<p>If the id matches up, we use the <code>.show()</code> jQuery function to display the div.  The argument 200 used in the <code>.show()</code> function will animate the display for a duration of 200 milliseconds.  How cool is that =)  If you leave it blank, the div will appear suddenly. </p>
<p>If the id does not match up, we simply use the <code>.hide()</code> jQuery function to hide the div.  The argument 600 used in the <code>.hide()</code> function will animate the div so that it looks like it is sliding away.  I used a higher number here so that you can see the difference in speed of the animations.  Again, you can simply leave it blank if you want to make the div suddenly disappear.</p>
<p>That&#8217;s it!</p>
<p>If you would like <strong>all the divs to be hidden</strong> from the get go, simply use CSS to hide them all like so:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="html" style="font-family:monospace;">.newboxes {
    display: none;
}</pre></td></tr></table></div>

<hr />
<p>Here is another quick example using the jQuery <code>.slideDown()</code> and <code>.slideUp()</code> functions.  </p>
<fieldset>
<legend>Demo</legend>
<table>
<tr>
<td>
<div style="border: 1px solid blue; background-color: #99CCFF; padding: 5px;">
            <a id="myHeader1" href="javascript:slideonlyone('newboxes1');" >slide this one only</a>
         </div>
<div class="newboxes2" id="newboxes1" style="border: 1px solid black; background-color: #CCCCCC; display: block;padding: 5px;">Div #1</div>
</td>
<td>
<div style="border: 1px solid blue; background-color: #99CCFF; padding: 5px;">
            <a id="myHeader2" href="javascript:slideonlyone('newboxes2');" >slide this one only</a>
         </div>
<div class="newboxes2" id="newboxes2" style="border: 1px solid black; background-color: #CCCCCC; display: none;padding: 5px;">Div #2</div>
</td>
<td>
<div style="border: 1px solid blue; background-color: #99CCFF; padding: 5px;">
            <a id="myHeader3" href="javascript:slideonlyone('newboxes3');" >slide this one only</a>
         </div>
<div class="newboxes2" id="newboxes3" style="border: 1px solid black; background-color: #CCCCCC; display: none;padding: 5px;">Div #3</div>
</td>
</tr>
</table>
</fieldset>
<p>Everything is the same here except we are calling the <code>.slideDown()</code> and <code>.slideUp()</code> functions instead of the <code>.show()</code> and <code>.hide()</code> functions respectively.  Even the animation duration is kept the same if you pass an argument to the function calls.</p>
<p>Here is the JavaScript code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> slideonlyone<span style="color: #009900;">&#40;</span>thechosenone<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.newboxes2'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>index<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>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;id&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> thechosenone<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
               $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slideDown</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #009900;">&#125;</span>
          <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
               $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slideUp</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">600</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #009900;">&#125;</span>
     <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Not much explanation needed here.  Same idea, different functions used.</p>
<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
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;table&gt;
   &lt;tr&gt;
      &lt;td&gt;
         &lt;div style=&quot;border: 1px solid blue; background-color: #99CCFF; padding: 5px;&quot;&gt;
            &lt;a id=&quot;myHeader1&quot; href=&quot;javascript:slideonlyone('newboxes1');&quot; &gt;slide this one only&lt;/a&gt;
         &lt;/div&gt;
         &lt;div class=&quot;newboxes2&quot; id=&quot;newboxes1&quot; style=&quot;border: 1px solid black; background-color: #CCCCCC; display: block;padding: 5px;&quot;&gt;Div #1&lt;/div&gt;
      &lt;/td&gt;
      &lt;td&gt;
         &lt;div style=&quot;border: 1px solid blue; background-color: #99CCFF; padding: 5px;&quot;&gt;
            &lt;a id=&quot;myHeader2&quot; href=&quot;javascript:slideonlyone('newboxes2');&quot; &gt;slide this one only&lt;/a&gt;
         &lt;/div&gt;
         &lt;div class=&quot;newboxes2&quot; id=&quot;newboxes2&quot; style=&quot;border: 1px solid black; background-color: #CCCCCC; display: none;padding: 5px;&quot;&gt;Div #2&lt;/div&gt;
      &lt;/td&gt;
      &lt;td&gt;
         &lt;div style=&quot;border: 1px solid blue; background-color: #99CCFF; padding: 5px;&quot;&gt;
            &lt;a id=&quot;myHeader3&quot; href=&quot;javascript:slideonlyone('newboxes3');&quot; &gt;slide this one only&lt;/a&gt;
         &lt;/div&gt;
         &lt;div class=&quot;newboxes2&quot; id=&quot;newboxes3&quot; style=&quot;border: 1px solid black; background-color: #CCCCCC; display: none;padding: 5px;&quot;&gt;Div #3&lt;/div&gt;
      &lt;/td&gt;
   &lt;/tr&gt;
&lt;/table&gt;</pre></td></tr></table></div>

<p>Same thing here except we are calling the new <code>slideonlyone()</code> function that we just defined.</p>
<p>If you want <strong>all your div content to be hidden</strong> from the get go, simple use CSS to hide them like so:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="html" style="font-family:monospace;">.newboxes {
    display: none;
}</pre></td></tr></table></div>

<hr />
Here is a demo for changing text in the link according to the display status of the div:</p>
<fieldset>
<legend>Demo &#8211; Toggle link text and toggling div</legend>
<p><a id="aTag" href="javascript:toggleAndChangeText();">Expanded text mode &#9660;</a></p>
<div id="divToToggle">Div content that will be shown or hidden.</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
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;a id=&quot;aTag&quot; href=&quot;javascript:toggleAndChangeText();&quot;&gt;
   Expanded text mode &amp;#9660;
&lt;/a&gt;
&lt;div id=&quot;divToToggle&quot;&gt;
   Content that will be shown or hidden.&lt;strong&gt;
&lt;/div&gt;</pre></td></tr></table></div>

<p>Here we have an a tag that has the text that we want to toggle as well as the div content.  Whenever the link text is clicked, it fires off the <strong>toggleAndChangeText()</strong> JavaScript function.  </p>
<p>Here is the JavaScript code for the toggleAndChangeText() function:</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="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> toggleAndChangeText<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#divToToggle'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toggle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#divToToggle'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'display'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'none'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#aTag'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Collapsed text mode &amp;#9658'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
     <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
          $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#aTag'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Expanded text mode &amp;#9660'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Whenever the <strong>toggleAndChangeText()</strong> is fired off, it will first toggle the div with our nice jQuery <strong>toggle()</strong> function in line 2.  Then it will test if the div content is hidden (or if the <strong>display</strong> attribute is set to <strong>none</strong>) in line 3.  If so, it will change the link to &#8220;Collapsed text mode &#9658&#8243;.  Otherwise, the link text will be changed to &#8220;Expanded text mode &#9660&#8243; in line 7.</p>
<p>To reverse the effect and start with the content hidden, all we have to do is the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;a id=&quot;aTag&quot; href=&quot;javascript:toggleAndChangeText();&quot;&gt;
   Collapsed text mode &amp;#9658
&lt;/a&gt;
&lt;div id=&quot;divToToggle&quot; style=&quot;display: none;&quot;&gt;
   Content that will be shown or hidden.&lt;strong&gt;
&lt;/div&gt;</pre></td></tr></table></div>

<p>The inline CSS is the key to making this work.  We start with the content hidden which our JavaScript will detect so it will toggle to make the content visible once the link is clicked.</p>
<hr />
<p>Next up is a pretty cool demo where we use the <code>.animate()</code> jQuery function to help toggle just about any element.  In this case, we&#8217;ll use an image of yours truly :)  </p>
<fieldset>
<legend>Demo</legend>
<div id="clickme" style="background-color: #333333; color: #FFFFFF; padding: 10px; width: 200px; cursor:pointer;">
  Click here to toggle me in and out =)
</div>
<p><img id="me" src="http://www.randomsnippets.com/wp-content/uploads/2011/04/2.png" alt="It&#039;s me....ah!!!" title="Allen Liu" width="100" height="77" class="alignnone size-full wp-image-552" style="border: none;" /><br />
</fieldset>
<p>Here is the HTML code:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="html" style="font-family:monospace;">&lt;div id=&quot;clickme&quot; style=&quot;background-color: #333333; color: #FFFFFF; padding: 10px; width: 200px; cursor:pointer;&quot;&gt;
  Click here to toggle me in and out =)
&lt;/div&gt;
&lt;img id=&quot;me&quot; src=&quot;http://www.randomsnippets.com/wp-content/uploads/2011/04/2.png&quot; alt=&quot;It&amp;#039;s me....ah!!!&quot; title=&quot;Allen Liu&quot; width=&quot;100&quot; height=&quot;77&quot; class=&quot;alignnone size-full wp-image-552&quot; style=&quot;border: none;&quot; /&gt;</pre></td></tr></table></div>

<p>I have to apologize for all the <a href="http://www.tizag.com/cssT/inline.php">inline CSS</a>.  This is the easiest way for me to get these demos across without having to separate the styling (which is usually best practice).  I am trying to do good here so I hope the <code>HTML Gods</code> will spare me this one time.</p>
<p>Anyway, the HTML is quite simple.  It is just a div with some text inside and a silly image.  Notice there is no JavaScript event attached to our div just yet.  This is where our jQuery code comes in and saves the day!</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="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#clickme'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#me'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
               height<span style="color: #339933;">:</span> <span style="color: #3366CC;">'toggle'</span>
               <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2000</span>
          <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>In line 1, we tell jQuery to wait until the DOM (Document Object Model) is fully loaded or when our page is fully rendered.  We do this because we want to add functionality to our &#8220;click me&#8221; div.  If the &#8220;click me&#8221; div has not yet loaded and this JavaScript executes, there is no element to work on.  Game over.  </p>
<p>Instead, we tell jQuery to wait until our &#8220;click me&#8221; div is in place, and then add a click event to it (line 2).</p>
<p>Lines 3-6 defines our click event by animating the <code>$('#me')</code> element which in this case is the image.  If you are not used to jQuery syntax, <code>$('#me')</code> uses <a href="http://api.jquery.com/category/selectors/">jQuery&#8217;s all-powerful selector</a> to help us select the element with <code>id = me</code>.  </p>
<p>Once we have our element selected, we apply the <code>animate</code> function to add animation to this element&#8217;s CSS properties.  We first begin with line 4 which toggles the height of the element and preserves the height:width ratio which gives it a shrinking effect.  We use toggle here so that you can keep pressing the button to toggle in and out.  You can of course use <code>show</code> or <code>hide</code> just like in the previous demos. </p>
<p>Line 5 just indicates how long the duration of our animation will last in milliseconds.  In this demo, it is 2000 milliseconds which translates to 2 seconds (I was in AP math in high school).</p>
<hr />
Here is a demo that toggles multiples elements by the class attribute.</p>
<fieldset>
<legend>Demo</legend>
<p><button onclick="toggleByClass('oddNumber');">Toggle odd numbers only</button><br />
<button onclick="toggleByClass('evenNumber');">Toggle even numbers only</button></p>
<div class="oddNumber">1</div>
<div class="evenNumber">2</div>
<div class="oddNumber">3</div>
<div class="evenNumber">4</div>
<div class="oddNumber">5</div>
<div class="evenNumber">6</div>
</fieldset>
<p>Here is the HTML code along with the CSS:</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
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;style type=&quot;text/css&quot;&gt;
.evenNumber, .oddNumber {
     width: 100px;
     height: 100px;
     margin: 10px;
     padding: 10px;
     float: left;     
     background-color: #333333;
     color: #FFFFFF;
}
&lt;/style&gt;
&lt;button onclick=&quot;toggleByClass('oddNumber');&quot;&gt;Toggle odd numbers only&lt;/button&gt;
&lt;button onclick=&quot;toggleByClass('evenNumber');&quot;&gt;Toggle even numbers only&lt;/button&gt;
&lt;div class=&quot;oddNumber&quot;&gt;1&lt;/div&gt;
&lt;div class=&quot;evenNumber&quot;&gt;2&lt;/div&gt;
&lt;div class=&quot;oddNumber&quot;&gt;3&lt;/div&gt;
&lt;div class=&quot;evenNumber&quot;&gt;4&lt;/div&gt;
&lt;div class=&quot;oddNumber&quot;&gt;5&lt;/div&gt;
&lt;div class=&quot;evenNumber&quot;&gt;6&lt;/div&gt;</pre></td></tr></table></div>

<p>Lines 1-11 covers the styling of the divs so they are presentable.</p>
<p>Lines 12-13 codes for 2 buttons that toggles the odd number divs and even number divs respectively by calling the <code>toggleByClass</code> function and passing the respective class names.</p>
<p>Here is the JavaScript code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> toggleByClass<span style="color: #009900;">&#40;</span>className<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.&quot;</span><span style="color: #339933;">+</span>className<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toggle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Simple and sweet but a lot is going on here.  Let&#8217;s dissect line 2 and cover each component separately.  </p>
<p><code>$("."+className)</code> uses the <a href="http://api.jquery.com/class-selector/" title="jQuery class selector" target="_blank">jQuery class selector</a> with the <code>className</code> variable representing the class of the element(s) you want to select.  In our demo, we use the <code>oddNumber</code> and <code>evenNumber</code> class names to select for their respective divs.</p>
<p>Once the divs are selected, we tack on the <code>.toggle()</code> jQuery function to hide and show the elements. </p>
<p>Here is a tutorial that shows <a href="http://www.randomsnippets.com/2011/10/09/how-to-hide-or-show-content-based-on-links-or-urls-via-javascript-jquery/">how to hide or show content based on links on different pages</a>.</p>
<p>That is it for yet another demo =)</p>
<p>If there any other demos/tutorials that you would like to see on this subject, please feel free to comment below and I will try my best to implement it.</p>
<p><strong>Note:</strong> If you have an issue with your code, please give me a URL to work with.  It&#8217;s extremely difficult for me to help if I cannot actually see the code.</p>
<p>If you found that my code was helpful in any way, shape, or form and would like to buy me a <strong>beer</strong>, please use the <strong>Donate</strong> button below =)  Cheers!<br />
<center></p>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick"><br />
<input type="hidden" name="hosted_button_id" value="WB5GF5WQ6YE5U"><br />
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"><br />
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1"><br />
</form>
<p></center></p>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2011/04/10/how-to-hide-show-or-toggle-your-div-with-jquery/feed/</wfw:commentRss>
		<slash:comments>273</slash:comments>
		</item>
		<item>
		<title>Parsing XML data from hostip.info API service with PHP</title>
		<link>http://www.randomsnippets.com/2011/02/03/parsing-xml-data-from-hostip-info-api-service-php/</link>
		<comments>http://www.randomsnippets.com/2011/02/03/parsing-xml-data-from-hostip-info-api-service-php/#comments</comments>
		<pubDate>Thu, 03 Feb 2011 08:35:04 +0000</pubDate>
		<dc:creator>Allen Liu</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[geolocation]]></category>
		<category><![CDATA[latitude]]></category>
		<category><![CDATA[longitude]]></category>

		<guid isPermaLink="false">http://www.randomsnippets.com/?p=294</guid>
		<description><![CDATA[If you ended up here, then you most likely experienced what I did in trying to figure out how to parse the XML data from the hostip.info web API service. Basically, it is down to the namespaces that are not &#8230; <a href="http://www.randomsnippets.com/2011/02/03/parsing-xml-data-from-hostip-info-api-service-php/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>If you ended up here, then you most likely experienced what I did in trying to figure out how to parse the XML data from the <a href="http://www.hostip.info/">hostip.info</a> web API service.</p>
<p>Basically, it is down to the namespaces that are not accessible by normal means.</p>
<p>Here is the basic XML response from a typical hostip.info call such as <code>http://api.hostip.info/?ip=[IP-ADDRESS]</code></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;ISO-8859-1&quot;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;HostipLookupResultSet</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0.1&quot;</span> <span style="color: #000066;">xmlns:gml</span>=<span style="color: #ff0000;">&quot;http://www.opengis.net/gml&quot;</span> <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span> <span style="color: #000066;">xsi:noNamespaceSchemaLocation</span>=<span style="color: #ff0000;">&quot;http://www.hostip.info/api/hostip-1.0.1.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;gml:description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>This is the Hostip Lookup Service<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/gml:description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;gml:name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>hostip<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/gml:name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;gml:boundedBy<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;gml:Null<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>inapplicable<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/gml:Null<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/gml:boundedBy<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;gml:featureMember<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Hostip<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ip<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>12.215.42.19<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ip<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;gml:name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Sugar Grove, IL<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/gml:name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;countryName<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>UNITED STATES<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/countryName<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;countryAbbrev<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>US<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/countryAbbrev<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #808080; font-style: italic;">&lt;!-- Co-ordinates are available as lng,lat --&gt;</span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ipLocation<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;gml:pointProperty<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
               <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;gml:Point</span> <span style="color: #000066;">srsName</span>=<span style="color: #ff0000;">&quot;http://www.opengis.net/gml/srs/epsg.xml#4326&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;gml:coordinates<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>-88.4588,41.7696<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/gml:coordinates<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
               <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/gml:Point<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/gml:pointProperty<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ipLocation<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Hostip<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/gml:featureMember<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/HostipLookupResultSet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p><span id="more-294"></span><br />
Here is a nice PHP function that will return all the important pieces for you:</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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> get_location<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ip</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://api.hostip.info/?ip='</span><span style="color: #339933;">.</span><span style="color: #000088;">$ip</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span> <span style="color: #339933;">!=</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SimpleXmlElement<span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$coordinates</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">children</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'gml'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">featureMember</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">children</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Hostip</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ipLocation</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">children</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'gml'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pointProperty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Point</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">coordinates</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$longlat</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">','</span><span style="color: #339933;">,</span> <span style="color: #000088;">$coordinates</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$location</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'longitude'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$longlat</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$location</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'latitude'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$longlat</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>		
                <span style="color: #000088;">$location</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'citystate'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'==&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">children</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'gml'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">featureMember</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">children</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Hostip</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">children</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'gml'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$location</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'country'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span>  <span style="color: #0000ff;">'==&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">children</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'gml'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">featureMember</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">children</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Hostip</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">countryName</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$location</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Here is the usage:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$ip</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'12.215.42.19'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$location_info</span> <span style="color: #339933;">=</span> get_location<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ip</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$location_info</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The dump would look something like this:</p>
<pre>
array(4) {
  ["longitude"]=>
  string(8) "-74.9072"
  ["latitude"]=>
  string(7) "39.9499"
  ["citystate"]=>
  object(SimpleXMLElement)#5 (1) {
    [0]=>
    string(16) "Mount Laurel, NJ"
  }
  ["country"]=>
  object(SimpleXMLElement)#2 (1) {
    [0]=>
    string(13) "UNITED STATES"
  }
}
</pre>
<p>To access the longitude:<br />
<code>$location_info['longitude'];</code></p>
<p>To access the latitude:<br />
<code>$location_info['latitude'];</code></p>
<p>To access the city/state string:<br />
<code>$location_info['citystate'];</code></p>
<p>To access the country string:<br />
<code>$location_info['country'];</code></p>
<p>I did not bother to parse the city/state string because I&#8217;m not sure if this format is consistent for international addresses.</p>
<p>Anyway, I hope this code helps someone out there =)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2011/02/03/parsing-xml-data-from-hostip-info-api-service-php/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Groupon &#8211; Commission Junction (CJ) WordPress Widget Plugin that Displays Local Deals</title>
		<link>http://www.randomsnippets.com/2011/01/15/groupon-cj-wordpress-plugin-displays-local-deals/</link>
		<comments>http://www.randomsnippets.com/2011/01/15/groupon-cj-wordpress-plugin-displays-local-deals/#comments</comments>
		<pubDate>Sat, 15 Jan 2011 17:33:38 +0000</pubDate>
		<dc:creator>Allen Liu</dc:creator>
				<category><![CDATA[WordPress Plugin]]></category>
		<category><![CDATA[cj]]></category>
		<category><![CDATA[commission junction]]></category>
		<category><![CDATA[groupon]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[widget]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.randomsnippets.com/?p=252</guid>
		<description><![CDATA[I have developed a Groupon -Commission Junction (CJ) WordPress Widget Plugin that automatically displays the local deals for the visitor. It allows you to plugin your Commission Junction (CJ) PID account number and display the local links according to the &#8230; <a href="http://www.randomsnippets.com/2011/01/15/groupon-cj-wordpress-plugin-displays-local-deals/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>I have developed a <strong>Groupon -Commission Junction (CJ) WordPress Widget Plugin</strong> that automatically displays the <strong>local deals</strong> for the visitor.  It allows you to plugin your Commission Junction (CJ) PID account number and display the local links according to the visitor IP address.  The <strong>demo of this widget</strong> is currently running on this site in the upper right-hand corner.<br />
<span id="more-252"></span><br />
<img src="http://www.randomsnippets.com/wp-content/uploads/2011/01/demo.png" alt="View Groupon-CJ WordPress Widget Demo" title="View Groupon-CJ WordPress Widget Demo" width="286" height="187" class="alignright size-full wp-image-271" style="border: none;" /></p>
<div style="clear: both;"></div>
<p>Here is the backend admin view:<br />
<a href="http://www.randomsnippets.com/wp-content/uploads/2011/01/Screen-shot-2011-01-15-at-8.43.54-AM.png"><img src="http://www.randomsnippets.com/wp-content/uploads/2011/01/Screen-shot-2011-01-15-at-8.43.54-AM.png" alt="Groupon-CJ WordPress Widget for Local Deals" title="Groupon-CJ WordPress Widget for Local Deals" width="272" height="363" class="size-full wp-image-253" /></a></p>
<div style="clear: both;"></div>
<p>The parameters that you can edit in the backend include:</p>
<ul>
<li><strong>CJ PID</strong> &#8211; This is your Commission Junction (CJ) PID number which allows the widget to display Groupon links connected to your CJ affiliate account.</li>
<li><strong>Title</strong> &#8211; This is the title of the widget which will be shown above the Groupon links list.  In the example given on this site, it is simply &#8220;Local Deals.&#8221;</li>
<li><strong>Max number of links displayed</strong> &#8211; This parameter allows you to set the maximum number of local deals or Groupon links you would like to display for your visitors.  Please note that it is not guaranteed that the maximum number of local deals will be listed every day.  That will depend on the availability of Groupon deals in the given area.</li>
<li><strong>Text to display if there are no deals found</strong> &#8211; Depending on where your visitor may be visiting from, there may be no local Groupon deals available or even setup in that location.  In these cases, this is the text that will be displayed.</li>
<li><strong>Loading text</strong> &#8211; This is the text that will be shown will the widget goes out and searched for your visitor&#8217;s local Groupon deals.  It is basically a busy indicator.</li>
</ul>
<p>There is a disclosure that I need to make here and that is regarding the IP address of your visitor.  It is not guaranteed that the visitor will actually be seeing local deals if their IP address is not local to their physical location.  In the example where a user uses a proxy server based in a different state (or even country), they will actually be seeing deals in the area of the proxy IP address.  This does not happen often but it does happen.</p>
<p>With that said, I plan to sell this <strong>fully supported plugin for $19</strong>.  If you are interested in purchasing it, please use the PayPal button below.  After the order is placed through, an email will be sent to you with the contents of the widget and further instructions for installation (which is very simple).  It has been tested for WordPress versions 2.8 and above.</p>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick"><br />
<input type="hidden" name="hosted_button_id" value="TKXYVWXPMCARC"><br />
<input type="image" style="border:none;" src="https://www.paypal.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"><br />
<img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1"><br />
</form>
<p>
If you have any questions, please either comment below or write me directly at allen dot liu at randomsnippets dot com.  I am also open to any customization requests for this widget.  Thanks!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2011/01/15/groupon-cj-wordpress-plugin-displays-local-deals/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Sorting 2D associative arrays in PHP</title>
		<link>http://www.randomsnippets.com/2009/07/13/sorting-2d-associative-arrays-in-php/</link>
		<comments>http://www.randomsnippets.com/2009/07/13/sorting-2d-associative-arrays-in-php/#comments</comments>
		<pubDate>Tue, 14 Jul 2009 06:18:52 +0000</pubDate>
		<dc:creator>Allen Liu</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[multi-dimensional associative array]]></category>
		<category><![CDATA[sort]]></category>

		<guid isPermaLink="false">http://www.randomsnippets.com/?p=238</guid>
		<description><![CDATA[Surprisingly, it took me a long time to find this solution so I decided to post it for anyone who had a situation similar to mine. Let&#8217;s say you have the following 2D associative array in PHP of fruits and &#8230; <a href="http://www.randomsnippets.com/2009/07/13/sorting-2d-associative-arrays-in-php/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Surprisingly, it took me a long time to find this solution so I decided to post it for anyone who had a situation similar to mine.</p>
<p>Let&#8217;s say you have the following 2D associative array in PHP of fruits and their corresponding prices and you want to have them sorted by price:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$fruits</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'product'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'banana'</span><span style="color: #339933;">,</span>
            <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'price'</span><span style="color: #009900;">&#93;</span>     <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">2.99</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'product'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'apple'</span><span style="color: #339933;">,</span>
            <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'price'</span><span style="color: #009900;">&#93;</span>     <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">1.99</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'product'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'durian'</span><span style="color: #339933;">,</span>  <span style="color: #666666; font-style: italic;">//these smell by the way - i do not know how people can like them =)</span>
            <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'price'</span><span style="color: #009900;">&#93;</span>     <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">19.99</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'product'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'starfruit'</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">//not too stinky but still stinky nonetheless</span>
            <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'price'</span><span style="color: #009900;">&#93;</span>     <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">5.99</span>
        <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><span id="more-238"></span><br />
The answer to this one involves the use of the PHP function, <strong>usort</strong>, which allows users to define their own comparison function for sorting arrays of all shapes and sizes.</p>
<p>For this example, our function would need to be defined as the following if we wanted to have the prices descending:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> sortDescending <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #339933;">,</span> <span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'price'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$b</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'price'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'price'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$b</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'price'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Here is the slightly different version of the function for an ascending sort:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> sortAscending <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #339933;">,</span> <span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'price'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$b</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'price'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'price'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$b</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'price'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>To put it all together in a descending sort, we have the following:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> sortDescending <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #339933;">,</span> <span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'price'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$b</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'price'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'price'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$b</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'price'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #990000;">usort</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fruits</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;sortDescending&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$fruits</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$fruit</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$fruit</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'product'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$fruit</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'price'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This would give us the following output:<br />
<code><br />
durian: 19.99<br />
starfruit: 5.99<br />
banana: 2.99<br />
apple: 1.99<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2009/07/13/sorting-2d-associative-arrays-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dynamic or on-the-fly percentage calculations with JavaScript</title>
		<link>http://www.randomsnippets.com/2009/07/12/dynamic-or-on-the-fly-percentage-calculations-with-javascript/</link>
		<comments>http://www.randomsnippets.com/2009/07/12/dynamic-or-on-the-fly-percentage-calculations-with-javascript/#comments</comments>
		<pubDate>Sun, 12 Jul 2009 21:34:37 +0000</pubDate>
		<dc:creator>Allen Liu</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[percent calculation]]></category>

		<guid isPermaLink="false">http://www.randomsnippets.com/?p=233</guid>
		<description><![CDATA[Here is a simple JavaScript function that does dynamic or on-the-fly percentage calculations. Demo MSRP (in dollars) Sale Price (in dollars) You have saved 50 % The savings percentage will change dynamically according to the values placed into the MSRP &#8230; <a href="http://www.randomsnippets.com/2009/07/12/dynamic-or-on-the-fly-percentage-calculations-with-javascript/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p><script lang="javascript">
function calculatePercentage (oldval, newval) {
    percentsavings = ((oldval - newval) / oldval) * 100;
    document.getElementById("results").innerHTML = Math.round(percentsavings*100)/100;
}
</script><br />
Here is a simple JavaScript function that does dynamic or on-the-fly percentage calculations.</p>
<fieldset>
<legend>Demo</legend>
<p><label for="msrp">MSRP (in dollars)</label><input type="text" id="msrp" name="msrp" size="5" value="200" onkeyup="calculatePercentage(this.value, document.getElementById('newprice').value)"><br />
<br/><br />
<label for="newprice">Sale Price (in dollars)</label><input type="text" id="newprice" name="newprice" size="5" value="100" onkeyup="calculatePercentage(document.getElementById('msrp').value, this.value)"><br />
<br/></p>
<div><strong>You have saved</strong> <span id="results" style="color: green; font-size: 1.5em;">50</span> <strong>%</strong></div>
</fieldset>
<p><span id="more-233"></span><br />
The savings percentage will change dynamically according to the values placed into the MSRP and sales price inputs.  The initial 50% percent value was hardcoded in this case to save time but the value will change accordingly.<br />
<br/><br />
Here is the plain HTML:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;label for=&quot;msrp&quot;&gt;MSRP&lt;/label&gt;&lt;input type=&quot;text&quot; id=&quot;msrp&quot; name=&quot;msrp&quot; size=&quot;5&quot; value=&quot;200&quot; onkeyup=&quot;calculatePercentage(this.value, document.getElementById('newprice').value)&quot;&gt;
&lt;br/&gt;
&lt;label for=&quot;newprice&quot;&gt;Sale Price&lt;/label&gt;&lt;input type=&quot;text&quot; id=&quot;newprice&quot; name=&quot;newprice&quot; size=&quot;5&quot; value=&quot;100&quot; onkeyup=&quot;calculatePercentage(document.getElementById('msrp').value, this.value)&quot;&gt;
&lt;br/&gt;
&lt;div&gt;&lt;strong&gt;You have saved&lt;/strong&gt; &lt;span id=&quot;results&quot; style=&quot;color: green; font-size: 1.5em;&quot;&gt;50&lt;/span&gt; &lt;strong&gt;%&lt;/strong&gt;&lt;/div&gt;</pre></td></tr></table></div>

<p>Lines 1 and 3 is where all the action is.  The JavaScript function, <strong>calculatePercentage</strong>, is executed everytime the <strong>onkeyup</strong> even listener fires and passes the <strong>MSRP</strong> and <strong>Sale Price</strong> arguments.  As you probably know already, the <strong>onkeyup</strong> event occurs everytime a keyboard key is released.  That is how we are constantly calculating and updating the percent savings.</p>
<p>Here is the JavaScript code for <strong>calculatePercentage</strong>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> calculatePercentage <span style="color: #009900;">&#40;</span>oldval<span style="color: #339933;">,</span> newval<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    percentsavings <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>oldval <span style="color: #339933;">-</span> newval<span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> oldval<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">;</span>
    document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;results&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="">Math</span>.<span style="color: #660066;">round</span><span style="color: #009900;">&#40;</span>percentsavings<span style="color: #339933;">*</span><span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #CC0000;">100</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This is a very simple percentage calculation function that places the results, that have been rounded to 2 decimal places, into the <strong>results</strong> div.  For the sake of brevity, there is no validation for the user input but the function will return <strong>NaN</strong> (Not a Number) if it is not able to calculate a percentage.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2009/07/12/dynamic-or-on-the-fly-percentage-calculations-with-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The dangers of embedding the notorious &#8220;void(0)&#8221; JavaScript code in the href attribute of the &#8220;a&#8221; tag</title>
		<link>http://www.randomsnippets.com/2009/04/08/the-dangers-of-embedding-the-notorious-void0-javascript-code-in-the-href-attribute-of-the-a-tag/</link>
		<comments>http://www.randomsnippets.com/2009/04/08/the-dangers-of-embedding-the-notorious-void0-javascript-code-in-the-href-attribute-of-the-a-tag/#comments</comments>
		<pubDate>Thu, 09 Apr 2009 04:50:27 +0000</pubDate>
		<dc:creator>Allen Liu</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[ie bug]]></category>
		<category><![CDATA[onClick]]></category>
		<category><![CDATA[void]]></category>

		<guid isPermaLink="false">http://www.randomsnippets.com/?p=212</guid>
		<description><![CDATA[I recent ran into an interesting IE bug involving the following bit of html code: &#60;a href=&#34;javascript: void(0);&#34; onclick=&#34;dosomething();&#34;&#62;click me&#60;/a&#62; I honestly did not write this one but I will leave names out of this to protect the innocent =) &#8230; <a href="http://www.randomsnippets.com/2009/04/08/the-dangers-of-embedding-the-notorious-void0-javascript-code-in-the-href-attribute-of-the-a-tag/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>I recent ran into an interesting IE bug involving the following bit of html code:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="html" style="font-family:monospace;">&lt;a href=&quot;javascript: void(0);&quot; onclick=&quot;dosomething();&quot;&gt;click me&lt;/a&gt;</pre></td></tr></table></div>

<p>I honestly did not write this one but I will leave names out of this to protect the innocent =)</p>
<p>The <strong>void(0)</strong> JavaScript code is usually used to prevent loading or reloading of the page when the user clicks the link.  </p>
<p>What we were trying to do here was have the <strong>dosomething</strong> JavaScript function execute when a user clicks on the link.  This works fine in FireFox, Chrome, Safari but not IE.  When a user clicks on the link in IE, nothing happens: i.e. (no pun intended) the JavaScript does not execute.  </p>
<p>It took us a good amount of time to realize what the issue was with IE.  It turns out that the <strong>void(0)</strong> function was preventing the <strong>onclick</strong> event to fire.</p>
<p>Although it may not be pretty, this is what we ended up doing and it appears to be cross-browser friendly:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="html" style="font-family:monospace;">&lt;a href=&quot;&lt;?=$_SERVER['PHP_SELF'];&gt;#&quot; onclick=&quot;dosomething(); return false;&quot;&gt;click me&lt;/a&gt;</pre></td></tr></table></div>

<p><span style="color: green;"><strong>Update:</strong></span> Here is an updated version of the code that accounts for users with JavaScript disabled:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="html" style="font-family:monospace;">&lt;a href=&quot;[alternative link]&quot; onclick=&quot;dosomething(); return false;&quot;&gt;click me&lt;/a&gt;</pre></td></tr></table></div>

<p>Thanks Ulrich for the suggestion!  In this version, if a user has JavaScript disabled, a click on this link will take them to the [alternative link].  If JavaScript is enabled, then <code>dosomething()</code> will execute and the return false will prevent the browser from going to the [alternative link].</p>
<p>Anyway, I hope this helps someone out there in the universe.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2009/04/08/the-dangers-of-embedding-the-notorious-void0-javascript-code-in-the-href-attribute-of-the-a-tag/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>How to randomly order or select rows in a MySQL query</title>
		<link>http://www.randomsnippets.com/2008/10/28/how-to-randomly-order-or-select-rows-in-a-mysql-query/</link>
		<comments>http://www.randomsnippets.com/2008/10/28/how-to-randomly-order-or-select-rows-in-a-mysql-query/#comments</comments>
		<pubDate>Wed, 29 Oct 2008 06:04:46 +0000</pubDate>
		<dc:creator>Allen Liu</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[LIMIT]]></category>
		<category><![CDATA[mysql query]]></category>
		<category><![CDATA[ORDER]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[RAND]]></category>
		<category><![CDATA[random selection]]></category>
		<category><![CDATA[SELECT]]></category>

		<guid isPermaLink="false">http://www.randomsnippets.com/?p=188</guid>
		<description><![CDATA[I was looking for a way to order the rows randomly in a MySQL query and the solution was not easy for me to come by. I am posting the solution here in hopes of helping others. 1 2 3 &#8230; <a href="http://www.randomsnippets.com/2008/10/28/how-to-randomly-order-or-select-rows-in-a-mysql-query/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>I was looking for a way to order the rows randomly in a MySQL query and the solution was not easy for me to come by.  I am posting the solution here in hopes of helping others.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> 
<span style="color: #990099; font-weight: bold;">FROM</span> my_table
<span style="color: #990099; font-weight: bold;">ORDER BY</span> <span style="color: #000099;">RAND</span><span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#41;</span></pre></td></tr></table></div>

<p><strong>RAND()</strong> returns a random floating-point value but functions to randomly order the selection of rows in the above usage.</p>
<p>If you combine the query with <strong>LIMIT</strong>, you will end up with a random selection of rows from your table.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> 
<span style="color: #990099; font-weight: bold;">FROM</span> my_table
<span style="color: #990099; font-weight: bold;">ORDER BY</span> <span style="color: #000099;">RAND</span><span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#41;</span>
<span style="color: #990099; font-weight: bold;">LIMIT</span> <span style="color: #008080;">3</span></pre></td></tr></table></div>

<p>Assuming you have more than 3 rows in your table, you will always get 3 random rows from the query.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/10/28/how-to-randomly-order-or-select-rows-in-a-mysql-query/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to convert MySQL timestamp to PHP date type</title>
		<link>http://www.randomsnippets.com/2008/10/05/how-to-convert-mysql-timestamp-to-php-date-type/</link>
		<comments>http://www.randomsnippets.com/2008/10/05/how-to-convert-mysql-timestamp-to-php-date-type/#comments</comments>
		<pubDate>Mon, 06 Oct 2008 05:01:52 +0000</pubDate>
		<dc:creator>Allen Liu</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[convert]]></category>
		<category><![CDATA[date function]]></category>
		<category><![CDATA[hh mm ss]]></category>
		<category><![CDATA[mysql timestamp]]></category>
		<category><![CDATA[parsing]]></category>
		<category><![CDATA[php date]]></category>
		<category><![CDATA[php strtotime]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[timestamp]]></category>
		<category><![CDATA[unix timestamp]]></category>

		<guid isPermaLink="false">http://www.randomsnippets.com/?p=131</guid>
		<description><![CDATA[Let&#8217;s say you have the following PHP code that extracts the date from the times table in your MySQL database. The date is of timestamp type which has the following format: &#8216;YYYY-MM-DD HH:MM:SS&#8217; or &#8217;2008-10-05 21:34:02.&#8217; 1 2 3 4 &#8230; <a href="http://www.randomsnippets.com/2008/10/05/how-to-convert-mysql-timestamp-to-php-date-type/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Let&#8217;s say you have the following PHP code that extracts the <strong>date</strong> from the <strong>times</strong> table in your MySQL database.  The <strong>date</strong> is of <strong>timestamp</strong> type which has the following format: &#8216;YYYY-MM-DD HH:MM:SS&#8217; or &#8217;2008-10-05 21:34:02.&#8217;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT date FROM times;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$res</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'date'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><span id="more-131"></span><br />
This date format that is in the output is in the timestamp format, <code>2008-10-05 21:34:02</code>, which is not surprising, but you want something that is more &#8220;user-friendly&#8221; or &#8220;readable&#8221; as in <strong>&#8220;9:34 pm October 5, 2008.&#8221;</strong></p>
<p>Let&#8217;s go back to the drawing board and try again:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT date FROM times;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$res</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;g:i a F j, Y &quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;date&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>A sample output from this PHP code would be <code>9:34 pm October 5, 2008</code> which is much more user-friendly.</p>
<p>The PHP <strong>strtotime</strong> function parses the MySQL timestamp into a Unix timestamp which can be utilized for further parsing or formatting in the PHP <strong>date</strong> function.</p>
<p>Here are some other sample date output formats that may be of practical use:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;F j, Y g:i a&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;date&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>                  <span style="color: #666666; font-style: italic;">// October 5, 2008 9:34 pm</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;m.d.y&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;date&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>                         <span style="color: #666666; font-style: italic;">// 10.05.08</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;j, n, Y&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;date&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>                       <span style="color: #666666; font-style: italic;">// 5, 10, 2008</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Ymd&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;date&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>                           <span style="color: #666666; font-style: italic;">// 20081005</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'\i\t \i\s \t\h\e jS \d\a\y.'</span><span style="color: #339933;">,</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;date&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   <span style="color: #666666; font-style: italic;">// It is the 5th day.</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;D M j G:i:s T Y&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;date&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>               <span style="color: #666666; font-style: italic;">// Sun Oct 5 21:34:02 PST 2008</span></pre></td></tr></table></div>

<p>If you found that my code was helpful in any way, shape, or form and would like to buy me a <strong>beer</strong>, please use the <strong>Donate</strong> button below =)  Cheers!</p>
<p><center></p>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick"><br />
<input type="hidden" name="hosted_button_id" value="WB5GF5WQ6YE5U"><br />
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"><br />
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1"><br />
</form>
<p></center></p>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/10/05/how-to-convert-mysql-timestamp-to-php-date-type/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>How to count values with MySQL queries</title>
		<link>http://www.randomsnippets.com/2008/10/05/how-to-count-values-with-mysql-queries/</link>
		<comments>http://www.randomsnippets.com/2008/10/05/how-to-count-values-with-mysql-queries/#comments</comments>
		<pubDate>Mon, 06 Oct 2008 03:40:35 +0000</pubDate>
		<dc:creator>Allen Liu</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[alias]]></category>
		<category><![CDATA[column values]]></category>
		<category><![CDATA[count values]]></category>
		<category><![CDATA[counter]]></category>
		<category><![CDATA[loop through]]></category>
		<category><![CDATA[mysql queries]]></category>
		<category><![CDATA[mysql query]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[value]]></category>
		<category><![CDATA[vote counters]]></category>

		<guid isPermaLink="false">http://www.randomsnippets.com/?p=116</guid>
		<description><![CDATA[Let&#8217;s say you have the following table called votes that keeps track of how people voted and you want a query to count the number of votes for you instead of having to loop through all the rows with a &#8230; <a href="http://www.randomsnippets.com/2008/10/05/how-to-count-values-with-mysql-queries/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Let&#8217;s say you have the following table called <strong>votes</strong> that keeps track of how people voted and you want a query to count the number of votes for you instead of having to loop through all the rows with a counter in PHP.  </p>
<table>
<tr>
<th>person</th>
<th>vote</th>
</tr>
<tr>
<td>obama</td>
<td>yes</td>
</tr>
<tr>
<td>mccain</td>
<td>no</td>
</tr>
<tr>
<td>obama</td>
<td>yes</td>
</tr>
<tr>
<td>obama</td>
<td>no</td>
</tr>
<tr>
<td>mccain</td>
<td>yes</td>
</tr>
<tr>
<td>obama</td>
<td>yes</td>
</tr>
<tr>
<td>obama</td>
<td>yes</td>
</tr>
<tr>
<td>obama</td>
<td>no</td>
</tr>
<tr>
<td>mccain</td>
<td>no</td>
</tr>
</table>
<p><span id="more-116"></span></p>
<p>Here is the MySQL query that would do just the job.<br />
<code><br />
SELECT person,<br />
SUM(IF(vote = "yes", 1,0)) AS `yes_votes`,<br />
SUM(IF(vote = "no", 1,0)) AS `no_votes`,<br />
COUNT(vote) AS `total`<br />
FROM votes<br />
GROUP BY person<br />
ORDER BY yes_votes DESC<br />
</code></p>
<p>The following would be the result of the query:</p>
<table>
<tr>
<th>person</th>
<th>yes_votes</th>
<th>no_votes</th>
<th>total</th>
</tr>
<tr>
<td>obama</td>
<td>4</td>
<td>2</td>
<td>6</td>
</tr>
<tr>
<td>mccain</td>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
</table>
<p>The beauty of the query all lies in the <strong>SUM IF</strong> statements.  For example, in order to count the <strong>yes</strong> votes, the IF statement will check to see if <strong>vote = &#8220;yes&#8221;</strong> as it loops through all the rows.  If so, the <strong>yes_votes</strong> column alias is incremented by <strong>1</strong>.  The same procedure goes for counting the no votes.  The <strong>COUNT</strong> statements keeps a tally on the total number of votes.  The <strong>GROUP BY person</strong> statement allows the vote counters to calculate the numbers by person instead of the total number of yes and no votes.  </p>
<p>Now, you do not have to loop through your MySQL results to count column values =)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/10/05/how-to-count-values-with-mysql-queries/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>How to confirm or prompt user for input via JavaScript</title>
		<link>http://www.randomsnippets.com/2008/06/26/how-to-confirm-or-prompt-user-for-input-via-javascript/</link>
		<comments>http://www.randomsnippets.com/2008/06/26/how-to-confirm-or-prompt-user-for-input-via-javascript/#comments</comments>
		<pubDate>Fri, 27 Jun 2008 06:59:11 +0000</pubDate>
		<dc:creator>Allen Liu</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[confirm]]></category>
		<category><![CDATA[dialog box]]></category>
		<category><![CDATA[pop up box]]></category>
		<category><![CDATA[prompt]]></category>
		<category><![CDATA[user input]]></category>

		<guid isPermaLink="false">http://www.randomsnippets.com/?p=21</guid>
		<description><![CDATA[JavaScript has a built-in function called confirm which takes a string argument that poses the question to the user and gives them the option to click the OK or Cancel buttons and returns true if the user clicks OK. Demo &#8230; <a href="http://www.randomsnippets.com/2008/06/26/how-to-confirm-or-prompt-user-for-input-via-javascript/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p><script language="javascript">
function removeYellowBox() {
    var parentBox = document.getElementById('parentBox');
    var yellowBox = document.getElementById('yellowBox');
    parentBox.removeChild(yellowBox);
}
</script><br />
JavaScript has a built-in function called <b>confirm</b> which takes a string argument that poses the question to the user and gives them the option to click the <b>OK</b> or <b>Cancel</b> buttons and returns true if the user clicks <b>OK</b>.</p>
<fieldset>
<legend>Demo</legend>
<div id="parentBox" style="background-color: green;padding:10px;">
    green box</p>
<div id="yellowBox" style="background-color: yellow;padding:10px;">
        yellow box
    </div>
</div>
<p><input type="button" value="Remove the yellow box" onClick="var answer = confirm('Are you sure you want to remove the yellow box?'); if (answer) { removeYellowBox(); }"><br />
</fieldset>
<p><span id="more-21"></span><br />
Here is the HTML code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;div id=&quot;parentDiv&quot; style=&quot;background-color: green;padding:10px;&quot;&gt;
    green box
    &lt;div id=&quot;yellowBox&quot; style=&quot;background-color: yellow;padding:10px;&quot;&gt;
        yellow box
    &lt;/div&gt;
&lt;/div&gt;
&lt;input type=&quot;button&quot; value=&quot;Remove the yellow box&quot; onClick=&quot;var answer = confirm('Are you sure you want to remove the yellow box?'); if (answer) { removeYellowBox(); }&quot;&gt;</pre></td></tr></table></div>

<p>The magic is in line 7 where we use <b>confirm</b> to ask a user if he or she really wants to delete our yellow box and retrieves the returned value and saves it in the <b>answer</b> variable.  We then test if the <b>answer</b> variable is true and, if so, execute the <b>removeYellowBox</b> function. </p>
<p>In addition to the <b>confirm</b> function, JavaScript also has another built-in function called <b>prompt</b> which prompts the user for input.  </p>
<fieldset>
<legend>Demo</legend>
<p><input type="button" value="prompt user" onClick="var answer = prompt('What is your favorite OS?', 'OSX'); alert('You said ' + answer + '!');"<br />
</fieldset>
<p>Here is the HTML:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;input type=&quot;button&quot; value=&quot;prompt user&quot; onClick=&quot;var answer = prompt('What is your favorite OS?', 'OSX'); alert('You said ' + answer + '!');&quot;</pre></td></tr></table></div>

<p>The <b>prompt</b> function will take 2 arguments: the string text for the user to see in the dialog box and the default value for the input. It also returns the user&#8217;s input which we have nicely saved into our <b>answer</b> variable.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/06/26/how-to-confirm-or-prompt-user-for-input-via-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to find and access parent nodes via JavaScript</title>
		<link>http://www.randomsnippets.com/2008/06/26/how-to-find-and-access-parent-nodes-via-javascript/</link>
		<comments>http://www.randomsnippets.com/2008/06/26/how-to-find-and-access-parent-nodes-via-javascript/#comments</comments>
		<pubDate>Fri, 27 Jun 2008 06:15:53 +0000</pubDate>
		<dc:creator>Allen Liu</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[dom tree]]></category>
		<category><![CDATA[findParentNode]]></category>
		<category><![CDATA[onClick]]></category>
		<category><![CDATA[onclick event]]></category>
		<category><![CDATA[parent node]]></category>
		<category><![CDATA[parent nodes]]></category>
		<category><![CDATA[parentName]]></category>
		<category><![CDATA[parentNode]]></category>
		<category><![CDATA[tagName]]></category>

		<guid isPermaLink="false">http://www.randomsnippets.com/?p=19</guid>
		<description><![CDATA[Have you ever had to access a parent node from a given child node? In this example, we are going to traverse up the DOM tree starting from the button to look for the parent node with the name attribute &#8230; <a href="http://www.randomsnippets.com/2008/06/26/how-to-find-and-access-parent-nodes-via-javascript/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p><script language="javascript">
function findParentNode(parentName, childObj) {
    var testObj = childObj.parentNode;
    var count = 1;
    while(testObj.getAttribute('name') != parentName) {
        alert('My name is ' + testObj.getAttribute('name') + '. Let\'s try moving up one level to see what we get.');
        testObj = testObj.parentNode;
        count++;
    }
    // now you have the object you are looking for - do something with it
    alert('Finally found ' + testObj.getAttribute('name') + ' after going up ' + count + ' level(s) through the DOM tree');
}
</script><br />
Have you ever had to access a parent node from a given child node?  In this example, we are going to traverse up the DOM tree starting from the button to look for the parent node with the <b>name</b> attribute <b>itsMe</b>.</p>
<fieldset>
<legend>Demo</legend>
<div name="notMe" style="padding:10px;border:1px solid green;">
My name is <b>notMe</b></p>
<div name="itsMe" style="padding:10px;border:1px solid blue;">
    My name is <b>itsMe</b></p>
<div name="notMeEither" style="padding:10px;border:1px solid red;">
        My name is <b>notMeEither</b></p>
<div name="tryAgain" style="padding:10px;border:1px solid purple;">
            My name is <b>tryAgain</b></p>
<div name="sorryNotMe" style="padding:10px;border:1px solid black;">
                My name is <b>sorryNotMe</b></p>
<div name="nopeSorry" style="padding:10px;border:1px solid brown;">
                    My name is <b>nopeSorry</b><br />
                        <input type="button" value="Find Parent Object with name=itsMe" onClick="findParentNode('itsMe', this);">
                    </div>
</p></div>
</p></div>
</p></div>
</p></div>
</div>
</fieldset>
<p><span id="more-19"></span><br />
Here is the HTML code.  It&#8217;s just a series of embedded div elements with different names and the input button where we start to traverse up the DOM tree accessing each parent node and asking for it&#8217;s name attribute value. The <b>onClick</b> event of the button triggers the <b>findParentNode</b> and passes the name of the element we are looking for and the object itself which, in this case, is the button.  This is the starting point where we will begin traversing up the DOM tree by accessing the parent nodes of each element and seeing if the name attribute matches what we are looking for.</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
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;div name=&quot;notMe&quot; style=&quot;padding:10px;border:1px solid green;&quot;&gt;
My name is &lt;b&gt;notMe&lt;/b&gt;
    &lt;div name=&quot;itsMe&quot; style=&quot;padding:10px;border:1px solid blue;&quot;&gt;
    My name is &lt;b&gt;itsMe&lt;/b&gt;
        &lt;div name=&quot;notMeEither&quot; style=&quot;padding:10px;border:1px solid red;&quot;&gt;
        My name is &lt;b&gt;notMeEither&lt;/b&gt;
            &lt;div name=&quot;tryAgain&quot; style=&quot;padding:10px;border:1px solid purple;&quot;&gt;
            My name is &lt;b&gt;tryAgain&lt;/b&gt;
                &lt;div name=&quot;sorryNotMe&quot; style=&quot;padding:10px;border:1px solid black;&quot;&gt;
                My name is &lt;b&gt;sorryNotMe&lt;/b&gt;
                    &lt;div name=&quot;nopeSorry&quot; style=&quot;padding:10px;border:1px solid brown;&quot;&gt;
                    My name is &lt;b&gt;nopeSorry&lt;/b&gt;
                        &lt;input type=&quot;button&quot; value=&quot;Find Parent Object with name=itsMe&quot; onClick=&quot;findParentNode('itsMe', this);&quot;&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;</pre></td></tr></table></div>

<p>Here is the <b>findParentNode</b> JavaScript code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> findParentNode<span style="color: #009900;">&#40;</span>parentName<span style="color: #339933;">,</span> childObj<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">var</span> testObj <span style="color: #339933;">=</span> childObj.<span style="color: #660066;">parentNode</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">var</span> count <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
    while<span style="color: #009900;">&#40;</span>testObj.<span style="color: #660066;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'name'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> parentName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        alert<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'My name is '</span> <span style="color: #339933;">+</span> testObj.<span style="color: #660066;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'name'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'. Let<span style="color: #000099; font-weight: bold;">\'</span>s try moving up one level to see what we get.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        testObj <span style="color: #339933;">=</span> testObj.<span style="color: #660066;">parentNode</span><span style="color: #339933;">;</span>
        count<span style="color: #339933;">++;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #006600; font-style: italic;">// now you have the object you are looking for - do something with it</span>
    alert<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Finally found '</span> <span style="color: #339933;">+</span> testObj.<span style="color: #660066;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'name'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">' after going up '</span> <span style="color: #339933;">+</span> count <span style="color: #339933;">+</span> <span style="color: #3366CC;">' level(s) through the DOM tree'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>In line 2, we access the parent node of the child element via the <b>parentNode</b> method which is the first div above the button named <b>nopeSorry</b>.  We then begin a count of the number of times we traversed up the DOM tree and set it to 1 because we have already accessed the parent node once.  The while loop will test if the name of the current node will match the name that we are looking for.  If not, it moves up the DOM tree by accessing the next parent node and continues to look for the right name while increasing the count by 1 each time.  Once it finds the right node, the function exits the while loop and alerts the node name along with the number of times it traversed up the DOM tree.</p>
<p>If you do not know the name of the parent element you are looking for you can also search using the <b>tagName</b> method.  The tagName will return the value of the tag name of the given node such as <b>A</b> or <b>DIV</b>.</p>
<p>If you found that my code was helpful in any way, shape, or form and would like to buy me a <strong>beer</strong>, please use the <strong>Donate</strong> button below =)  Cheers!</p>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick"><br />
<input type="hidden" name="hosted_button_id" value="P7VD7JXQGY2YJ"><br />
<input type="image" style="border: none;" src="https://www.paypalobjects.com/WEBSCR-640-20110306-1/en_US/i/btn/btn_donateCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"><br />
<img alt="" border="0" src="https://www.paypalobjects.com/WEBSCR-640-20110306-1/en_US/i/scr/pixel.gif" width="1" height="1"><br />
</form>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/06/26/how-to-find-and-access-parent-nodes-via-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to loop through checkboxes or radio button groups via JavaScript</title>
		<link>http://www.randomsnippets.com/2008/05/15/how-to-loop-through-checkboxes-or-radio-button-groups-via-javascript/</link>
		<comments>http://www.randomsnippets.com/2008/05/15/how-to-loop-through-checkboxes-or-radio-button-groups-via-javascript/#comments</comments>
		<pubDate>Fri, 16 May 2008 05:00:18 +0000</pubDate>
		<dc:creator>Allen Liu</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[checkboxes]]></category>
		<category><![CDATA[dom object]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[javascript code]]></category>
		<category><![CDATA[javascript function]]></category>
		<category><![CDATA[loop through]]></category>
		<category><![CDATA[radio button groups]]></category>

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

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

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

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

<p>This function loops through all the elements in the form that was passed over to the function.  </p>
<p>If the element is of <b>checkbox</b> type and it is checked, the value of this element will be appended to our <b>cbResults</b> variable which is keeping track of our checkbox results.</p>
<p>If the element is of <b>radio</b> type and it is checked, the value of this element will be appended to our <b>radioResults</b> variable which is keeping track of our radio button results.</p>
<p>After all the looping is finished, the results are <b>cbResults</b> and <b>radioResults</b> are inserted into the <b>cbResults div</b> and <b>radioResults div</b> respectively.</p>
<p>If you found that my code was helpful in any way, shape, or form and would like to buy me a <strong>beer</strong>, please use the <strong>Donate</strong> button below =)  Cheers!</p>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick"><br />
<input type="hidden" name="hosted_button_id" value="P7VD7JXQGY2YJ"><br />
<input type="image" style="border: none;" src="https://www.paypalobjects.com/WEBSCR-640-20110306-1/en_US/i/btn/btn_donateCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"><br />
<img alt="" border="0" src="https://www.paypalobjects.com/WEBSCR-640-20110306-1/en_US/i/scr/pixel.gif" width="1" height="1"><br />
</form>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/05/15/how-to-loop-through-checkboxes-or-radio-button-groups-via-javascript/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>How to create your own customized calculator via JavaScript</title>
		<link>http://www.randomsnippets.com/2008/04/26/how-to-create-your-own-customized-calculator-via-javascript/</link>
		<comments>http://www.randomsnippets.com/2008/04/26/how-to-create-your-own-customized-calculator-via-javascript/#comments</comments>
		<pubDate>Sun, 27 Apr 2008 01:01:51 +0000</pubDate>
		<dc:creator>Allen Liu</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[addition and subtraction]]></category>
		<category><![CDATA[calculator]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[input buttons]]></category>
		<category><![CDATA[input object]]></category>
		<category><![CDATA[javascript code]]></category>
		<category><![CDATA[multiplication]]></category>
		<category><![CDATA[onclick event]]></category>

		<guid isPermaLink="false">http://www.randomsnippets.com/?p=15</guid>
		<description><![CDATA[This is a simple example of implementing a calculator with JavaScript and HTML. This calculator will only have the division, multiplication, addition, and subtraction operators but you can easily tack on more functions if needed. Demo Here is the html &#8230; <a href="http://www.randomsnippets.com/2008/04/26/how-to-create-your-own-customized-calculator-via-javascript/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p><script lang="javascript">
function calculate(equation) {
     var answer = eval(equation);
     document.getElementById('screen').value = answer;
}
function pushButton(buttonValue) {
     if (buttonValue == 'C') {
          document.getElementById('screen').value = '';
     }
     else {
          document.getElementById('screen').value += buttonValue;
     }
}
</script></p>
<style type="text/css">
table.calc {
     border: 2px solid #0034D1;
     background-color: #809FFF;
}
input.calc {
     width: 100%;
     margin: 5px;
}
tr.calc {
     border: 0px;
}
</style>
<p>This is a simple example of implementing a calculator with JavaScript and HTML.  This calculator will only have the division, multiplication, addition, and subtraction operators but you can easily tack on more functions if needed.</p>
<fieldset>
<legend>Demo</legend>
<table class="calc" cellpadding=4>
<tr class="calc">
<td colspan=3><input class="calc" id="screen" type="text"></td>
</tr>
<tr class="calc">
<td><input class="calc" type="button" value=1 onclick="pushButton(this.value);"></td>
<td><input class="calc" type="button" value=2 onclick="pushButton(this.value);"></td>
<td><input class="calc" type="button" value=3 onclick="pushButton(this.value);"></td>
<td><input class="calc" type="button" value='/' onclick="pushButton(this.value);"></td>
</tr>
<tr>
<td><input class="calc" type="button" value=4 onclick="pushButton(this.value);"></td>
<td><input class="calc" type="button" value=5 onclick="pushButton(this.value);"></td>
<td><input class="calc" type="button" value=6 onclick="pushButton(this.value);"></td>
<td><input class="calc" type="button" value='*' onclick="pushButton(this.value);"></td>
</tr>
<tr>
<td><input class="calc" type="button" value=7 onclick="pushButton(this.value);"></td>
<td><input class="calc" type="button" value=8 onclick="pushButton(this.value);"></td>
<td><input class="calc" type="button" value=9 onclick="pushButton(this.value);"></td>
<td><input class="calc" type="button" value='-' onclick="pushButton(this.value);"></td>
</tr>
<tr>
<td><input class="calc" type="button" value=0 onclick="pushButton(this.value);"></td>
<td><input class="calc" type="button" value='.' onclick="pushButton(this.value);"></td>
<td><input class="calc" type="button" value='C' onclick="pushButton(this.value);"></td>
<td><input class="calc" type="button" value='+' onclick="pushButton(this.value);"></td>
</tr>
<tr>
<td colspan=3><input class="calc" type="button" value='=' onclick="calculate(document.getElementById('screen').value);"></td>
</tr>
</table>
</fieldset>
<p><span id="more-15"></span><br />
Here is the html code including the CSS stuff:</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
36
37
38
39
40
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;style type=&quot;text/css&quot;&gt;
table.calc {
     border: 2px solid #0034D1;
     background-color: #809FFF;
}
input.calc {
     width: 100%;
     margin: 5px;
}
&lt;/style&gt;
&lt;fieldset&gt;
&lt;legend&gt;Demo&lt;/legend&gt;
&lt;table class=&quot;calc&quot; cellpadding=4&gt;
&lt;tr&gt;&lt;td colspan=3&gt;&lt;input class=&quot;calc&quot; id=&quot;screen&quot; type=&quot;text&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;input class=&quot;calc&quot; type=&quot;button&quot; value=1 onclick=&quot;pushButton(this.value);&quot;&gt;&lt;/td&gt;
&lt;td&gt;&lt;input class=&quot;calc&quot; type=&quot;button&quot; value=2 onclick=&quot;pushButton(this.value);&quot;&gt;&lt;/td&gt;
&lt;td&gt;&lt;input class=&quot;calc&quot; type=&quot;button&quot; value=3 onclick=&quot;pushButton(this.value);&quot;&gt;&lt;/td&gt;
&lt;td&gt;&lt;input class=&quot;calc&quot; type=&quot;button&quot; value='/' onclick=&quot;pushButton(this.value);&quot;&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;input class=&quot;calc&quot; type=&quot;button&quot; value=4 onclick=&quot;pushButton(this.value);&quot;&gt;&lt;/td&gt;
&lt;td&gt;&lt;input class=&quot;calc&quot; type=&quot;button&quot; value=5 onclick=&quot;pushButton(this.value);&quot;&gt;&lt;/td&gt;
&lt;td&gt;&lt;input class=&quot;calc&quot; type=&quot;button&quot; value=6 onclick=&quot;pushButton(this.value);&quot;&gt;&lt;/td&gt;
&lt;td&gt;&lt;input class=&quot;calc&quot; type=&quot;button&quot; value='*' onclick=&quot;pushButton(this.value);&quot;&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;input class=&quot;calc&quot; type=&quot;button&quot; value=7 onclick=&quot;pushButton(this.value);&quot;&gt;&lt;/td&gt;
&lt;td&gt;&lt;input class=&quot;calc&quot; type=&quot;button&quot; value=8 onclick=&quot;pushButton(this.value);&quot;&gt;&lt;/td&gt;
&lt;td&gt;&lt;input class=&quot;calc&quot; type=&quot;button&quot; value=9 onclick=&quot;pushButton(this.value);&quot;&gt;&lt;/td&gt;
&lt;td&gt;&lt;input class=&quot;calc&quot; type=&quot;button&quot; value='-' onclick=&quot;pushButton(this.value);&quot;&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;input class=&quot;calc&quot; type=&quot;button&quot; value=0 onclick=&quot;pushButton(this.value);&quot;&gt;&lt;/td&gt;
&lt;td&gt;&lt;input class=&quot;calc&quot; type=&quot;button&quot; value='.' onclick=&quot;pushButton(this.value);&quot;&gt;&lt;/td&gt;
&lt;td&gt;&lt;input class=&quot;calc&quot; type=&quot;button&quot; value='C' onclick=&quot;pushButton(this.value);&quot;&gt;&lt;/td&gt;
&lt;td&gt;&lt;input class=&quot;calc&quot; type=&quot;button&quot; value='+' onclick=&quot;pushButton(this.value);&quot;&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=3&gt;&lt;input class=&quot;calc&quot; type=&quot;button&quot; value='=' onclick=&quot;calculate(document.getElementById('screen').value);&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</pre></td></tr></table></div>

<p>Most of the HTML is coding for all the input buttons and their associated <b>onclick</b> event handlers.  All the buttons, except for the <b>=</b> button, will call the <b>pushButton</b> JavaScript when clicked and passes it&#8217;s associated value.  The <b>=</b> button calls the <b>calculate</b> JavaScript function and passes the value that is in the text input which represents the string of numbers and operators that were pushed.  The rest of the HTML is just icing on the cake so that it actually looks like a calculator.</p>
<p>Here is the JavaScript code for the 2 functions:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> pushButton<span style="color: #009900;">&#40;</span>buttonValue<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>buttonValue <span style="color: #339933;">==</span> <span style="color: #3366CC;">'C'</span><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;">'screen'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">value</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
     <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
          document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'screen'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">value</span> <span style="color: #339933;">+=</span> buttonValue<span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000066; font-weight: bold;">function</span> calculate<span style="color: #009900;">&#40;</span>equation<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #000066; font-weight: bold;">var</span> answer <span style="color: #339933;">=</span> eval<span style="color: #009900;">&#40;</span>equation<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'screen'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">value</span> <span style="color: #339933;">=</span> answer<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The <b>pushButton</b> function does 2 things for us.  If the user clicks on the <b>C</b> button or the clear button, it accesses the text input object, which represents our calculator screen, via the DOM .  Once it is accessed, the value inside the text input is set to &#8221; (2 single quotes) which is an empty string.  This will basically remove whatever string is already inside the text input.  If the user clicks on any other button, the function will access the text input object and append the value that was passed over to whatever is already in our text input.  This allows us to build our string with numbers and operators.</p>
<p>The <b>calculate</b> function does exactly what it&#8217;s name suggests.  It takes our string of numbers and operators and evaluates it in line 10.  The <b>eval()</b> function treats the string as if it were JavaScript code and executes it and returns the results.  The function will then access the text input object and replace whatever the current value is with the answer from the <b>eval()</b> function.</p>
<p>That&#8217;s it!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/04/26/how-to-create-your-own-customized-calculator-via-javascript/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>How to dynamically add content to a div and store the content to a cookie via JavaScript</title>
		<link>http://www.randomsnippets.com/2008/04/14/how-to-dynamically-add-content-to-a-div-via-javascript/</link>
		<comments>http://www.randomsnippets.com/2008/04/14/how-to-dynamically-add-content-to-a-div-via-javascript/#comments</comments>
		<pubDate>Tue, 15 Apr 2008 06:45:17 +0000</pubDate>
		<dc:creator>Allen Liu</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[add]]></category>
		<category><![CDATA[content]]></category>
		<category><![CDATA[cookieContent]]></category>
		<category><![CDATA[demo]]></category>
		<category><![CDATA[demo content]]></category>
		<category><![CDATA[div id]]></category>
		<category><![CDATA[document cookie]]></category>
		<category><![CDATA[document getelementbyid]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[dynamic content]]></category>
		<category><![CDATA[Dynamically]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[getCookie]]></category>
		<category><![CDATA[getElementById]]></category>
		<category><![CDATA[input text]]></category>
		<category><![CDATA[javascript code]]></category>
		<category><![CDATA[javascript function]]></category>
		<category><![CDATA[javascript functions]]></category>
		<category><![CDATA[onClick]]></category>
		<category><![CDATA[setCookie]]></category>
		<category><![CDATA[store content]]></category>

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

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;form name=&quot;myForm&quot;&gt;
Content to be added:
&lt;textarea name=&quot;myContent&quot;&gt;Type here...&lt;h1&gt;tags work as well&lt;/h1&gt;&lt;u&gt;woohoo&lt;/u&gt;&lt;/textarea&gt;
&lt;input type=&quot;button&quot; value=&quot;Add content&quot; onClick=&quot;addContent('myDiv', document.myForm.myContent.value); setCookie('content', document.myForm.myContent.value, 7);&quot;&gt;
&lt;/form&gt;
&lt;br&gt;&lt;br&gt;
Your content will be added dynamically below:
&lt;div id=&quot;myDiv&quot;&gt;&lt;/div&gt;</pre></td></tr></table></div>

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

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

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

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

<p>The <b>onClick</b> event listener will launch 2 scripts this time:</p>
<ol>
<li><b>addContent</b> &#8211; This will do exactly what it did before in the first demo and just insert the input text value inside the div.</li>
<li><b>setCookie</b> &#8211; This will save a cookie with the name <b>content</b> and assign whatever is inside the input text as the value.  In addition, the cookie is set to expire in 7 days.</li>
</ol>
<p>To avoid reinventing the wheel, I have borrowed the JavaScript get and set cookie functions from <a href="http://www.w3schools.com/JS/js_cookies.asp" target="_blank">W3 Schools</a> and edited it slightly for the needs of this demo.</p>
<p>Here are the new JavaScript functions for cookie management:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; 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: #000066; font-weight: bold;">var</span> exdate<span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">new</span> <span style="">Date</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    exdate.<span style="color: #660066;">setDate</span><span style="color: #009900;">&#40;</span>exdate.<span style="color: #660066;">getDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span>expiredays<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    document.<span style="color: #660066;">cookie</span><span style="color: #339933;">=</span>c_name<span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;=&quot;</span> <span style="color: #339933;">+</span>escape<span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>expiredays<span style="color: #339933;">==</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #3366CC;">&quot;&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;;expires=&quot;</span><span style="color: #339933;">+</span>exdate.<span style="color: #660066;">toGMTString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000066; font-weight: bold;">function</span> getCookie<span style="color: #009900;">&#40;</span>c_name<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">cookie</span>.<span style="color: #660066;">length</span><span style="color: #339933;">&gt;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        c_start<span style="color: #339933;">=</span>document.<span style="color: #660066;">cookie</span>.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span>c_name <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;=&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>c_start<span style="color: #339933;">!=-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
            c_start<span style="color: #339933;">=</span>c_start <span style="color: #339933;">+</span> c_name.<span style="color: #660066;">length</span><span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span> 
            c_end<span style="color: #339933;">=</span>document.<span style="color: #660066;">cookie</span>.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;;&quot;</span><span style="color: #339933;">,</span>c_start<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>c_end<span style="color: #339933;">==-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> c_end<span style="color: #339933;">=</span>document.<span style="color: #660066;">cookie</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">var</span> cookieContent <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Welcome back &quot;</span> <span style="color: #339933;">+</span> unescape<span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">cookie</span>.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span>c_start<span style="color: #339933;">,</span>c_end<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'myDiv2'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> cookieContent<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> 
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
getCookie<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'content'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

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

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

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

<p>Nothing exciting here except in line 6 where the <b>onClick</b> event is triggered when the user clicks on the button.  This event will launch the <b>verifyEmail()</b> JavaScript function which will check the user input of the email address.</p>
<p>Here is the code for the <b>verifyEmail()</b> function:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> verifyEmail<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #000066; font-weight: bold;">var</span> status <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>     
<span style="color: #000066; font-weight: bold;">var</span> emailRegEx <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i</span><span style="color: #339933;">;</span>
     <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">myform</span>.<span style="color: #660066;">email1</span>.<span style="color: #660066;">value</span>.<span style="color: #660066;">search</span><span style="color: #009900;">&#40;</span>emailRegEx<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          alert<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Please enter a valid email address.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
     <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">myform</span>.<span style="color: #660066;">email1</span>.<span style="color: #660066;">value</span> <span style="color: #339933;">!=</span> document.<span style="color: #660066;">myform</span>.<span style="color: #660066;">email2</span>.<span style="color: #660066;">value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          alert<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Email addresses do not match.  Please retype them to make sure they are the same.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
     <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
          alert<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Woohoo!  The email address is in the correct format and they are the same.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          status <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
     <span style="color: #000066; font-weight: bold;">return</span> status<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

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

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

<p>If you found that my code was helpful in any way, shape, or form and would like to buy me a <strong>beer</strong>, please use the <strong>Donate</strong> button below =)  Cheers!</p>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick"><br />
<input type="hidden" name="hosted_button_id" value="P7VD7JXQGY2YJ"><br />
<input type="image" style="border: none;" src="https://www.paypalobjects.com/WEBSCR-640-20110306-1/en_US/i/btn/btn_donateCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"><br />
<img alt="" border="0" src="https://www.paypalobjects.com/WEBSCR-640-20110306-1/en_US/i/scr/pixel.gif" width="1" height="1"><br />
</form>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/04/01/how-to-verify-email-format-via-javascript/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>How to dynamically remove/delete elements via JavaScript</title>
		<link>http://www.randomsnippets.com/2008/03/26/how-to-dynamically-remove-delete-elements-via-javascript/</link>
		<comments>http://www.randomsnippets.com/2008/03/26/how-to-dynamically-remove-delete-elements-via-javascript/#comments</comments>
		<pubDate>Thu, 27 Mar 2008 05:57:53 +0000</pubDate>
		<dc:creator>Allen Liu</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[getElementById]]></category>
		<category><![CDATA[onclick event]]></category>
		<category><![CDATA[remove]]></category>
		<category><![CDATA[removeChild]]></category>
		<category><![CDATA[removeElement]]></category>

		<guid isPermaLink="false">http://www.randomsnippets.com/2008/03/26/how-to-dynamically-removedelete-elements-via-javascript/</guid>
		<description><![CDATA[This post is in response to one of the comments on have received regarding the removal of elements via JavaScript. I have taken the original function posted and edited it a little bit for the demo. Demo I am the &#8230; <a href="http://www.randomsnippets.com/2008/03/26/how-to-dynamically-remove-delete-elements-via-javascript/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p><script lang="javascript">
function removeElement(parentDiv, childDiv){
     if (childDiv == parentDiv) {
          alert("The parent div cannot be removed.");
     }
     else if (document.getElementById(childDiv)) {     
          var child = document.getElementById(childDiv);
          var parent = document.getElementById(parentDiv);
          parent.removeChild(child);
     }
     else {
          alert("Child div has already been removed or does not exist.");
          return false;
     }
}
</script><br />
This post is in response to one of the <a href="/2008/02/21/how-to-dynamically-add-form-elements-via-javascript/#comment-10">comments on have received regarding the removal of elements via JavaScript</a>.  I have taken the original function posted and edited it a little bit for the demo.</p>
<fieldset>
<legend>Demo</legend>
<div id="parent" style="border: 1px solid red; padding: 10px;">
     I am the parent div.</p>
<div id="child" style="border: 1px solid green; padding: 10px;">
          I am a child div within the parent div.
     </div>
</div>
<p>&nbsp;</p>
<p><input type="button" value="Remove Element" onClick="removeElement('parent','child');"><br />
</fieldset>
<p><span id="more-12"></span><br />
Here is the plain HTML code:</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;div id=&quot;parent&quot; style=&quot;border: 1px solid red; padding: 10px;&quot;&gt;
     I am the parent div.
     &lt;div id=&quot;child&quot; style=&quot;border: 1px solid green; padding: 10px;&quot;&gt;
           I am a child div within the parent div.
     &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;input type=&quot;button&quot; value=&quot;Remove Element&quot; onClick=&quot;removeElement('parent','child');&quot;&gt;</pre></td></tr></table></div>

<p>The important thing to note here is that the <b>child div</b> is <b>within</b> the <b>parent div</b>.  Once the <b>Remove Element</b> button is clicked, the <b>onClick</b> event listener is triggered and launches the <b>removeElement</b> JavaScript function.  This functions accepts two arguments:</p>
<ul>
<li><b>parent</b> &#8211; This is just the <b>div id</b> of the parent div.</li>
<li><b>child</b> &#8211; This is the <b>div id</b> of the child div.</li>
</ul>
<p>Here is the code for the <b>removeElement</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
12
13
14
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">function removeElement(parentDiv, childDiv){
     if (childDiv == parentDiv) {
          alert(&quot;The parent div cannot be removed.&quot;);
     }
     else if (document.getElementById(childDiv)) {     
          var child = document.getElementById(childDiv);
          var parent = document.getElementById(parentDiv);
          parent.removeChild(child);
     }
     else {
          alert(&quot;Child div has already been removed or does not exist.&quot;);
          return false;
     }
}</pre></td></tr></table></div>

<p>The <b>if</b> block in lines 2-6 attempts to obtain the <b>child</b> element if it exists.  This is just in case the user has already removed the child div and is reattempting to it.  In this case, a JavaScript error would normally pop up because the child div does not exist.  </p>
<p>If the child element does exist, the first thing that we do is obtain the <b>child</b> element in line 3 using the <b>child div id</b>.  Next, we do the same thing with the <b>parent</b> element by using the <b>parent div id</b>  Lastly, we invoke the <b>removeChild</b> method from the parent element and pass the child element as the argument.  That&#8217;s it!</p>
<p>If the child div has already been removed, the <b>else</b> block in lines 7-10 gets executed.  In this case, an alert would tell the user that the child div has already been removed and the function would return a <b>false</b> value.</p>
<p>Here is another demo of this JavaScript function where you can type in the name of the child element to be removed and just click on the <b>Remove Element</b> button <b>OR</b> you can just click the button that corresponds to each given child div to remove it.</p>
<fieldset>
<legend>Demo</legend>
<div id="parentDiv" style="border: 1px solid red; padding: 10px;">
     My name is <b>parentDiv</b>.  I cannot be removed.</p>
<div id="child1" style="border: 1px solid green; padding: 10px; margin: 10px;">
          My name is <b>child1</b>.
     </div>
<div id="child2" style="border: 1px solid blue; padding: 10px; margin: 10px;">
          My name is <b>child2</b>.
     </div>
<div id="child3" style="border: 1px solid purple; padding: 10px; margin: 10px;">
          My name is <b>child3</b>.
     </div>
</div>
<p>&nbsp;</p>
<p>Name of child element to be removed: <input id="nameOfChild" type="text" value="child2"><input type="button" value="Remove Element" onClick="var name=document.getElementById('nameOfChild').value; removeElement('parentDiv', name);"></p>
<p>For those who are lazy in typing in the actual names, we have these handy-dandy buttons:<br />
<input type="button" value="Remove child1" onClick="removeElement('parentDiv', 'child1');"><br />
<input type="button" value="Remove child2" onClick="removeElement('parentDiv', 'child2');"><br />
<input type="button" value="Remove child3" onClick="removeElement('parentDiv', 'child3');"><br />
<input type="button" value="Remove parentDiv" onClick="removeElement('parentDiv', 'parentDiv');"><br />
</fieldset>
<p>We are using the same JavaScript function as above.  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
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;div id=&quot;parentDiv&quot; style=&quot;border: 1px solid red; padding: 10px;&quot;&gt;
     My name is &lt;b&gt;parentDiv&lt;/b&gt;.  I cannot be removed.
     &lt;div id=&quot;child1&quot; style=&quot;border: 1px solid green; padding: 10px; margin: 10px;&quot;&gt;
          My name is &lt;b&gt;child1&lt;/b&gt;.
     &lt;/div&gt;
     &lt;div id=&quot;child2&quot; style=&quot;border: 1px solid blue; padding: 10px; margin: 10px;&quot;&gt;
          My name is &lt;b&gt;child2&lt;/b&gt;.
     &lt;/div&gt;
     &lt;div id=&quot;child3&quot; style=&quot;border: 1px solid purple; padding: 10px; margin: 10px;&quot;&gt;
          My name is &lt;b&gt;child3&lt;/b&gt;.
     &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
Name of child element to be removed: &lt;input id=&quot;nameOfChild&quot; type=&quot;text&quot; value=&quot;child2&quot;&gt;&lt;input type=&quot;button&quot; value=&quot;Remove Element&quot; onClick=&quot;var name=document.getElementById('nameOfChild').value; removeElement('parentDiv', name);&quot;&gt;
&lt;br&gt;&lt;br&gt;
For those who are lazy in typing in the actual names, we have these handy-dandy buttons:
&lt;input type=&quot;button&quot; value=&quot;Remove child1&quot; onClick=&quot;removeElement('parentDiv', 'child1');&quot;&gt;
&lt;input type=&quot;button&quot; value=&quot;Remove child2&quot; onClick=&quot;removeElement('parentDiv', 'child2');&quot;&gt;
&lt;input type=&quot;button&quot; value=&quot;Remove child3&quot; onClick=&quot;removeElement('parentDiv', 'child3');&quot;&gt;
&lt;input type=&quot;button&quot; value=&quot;Remove parentDiv&quot; onClick=&quot;removeElement('parentDiv', 'parentDiv');&quot;&gt;</pre></td></tr></table></div>

<p>Here is a demo that removes child elements from the parent using checkboxes that a user would select.   This demo uses the same <b>removeElement</b> JavaScript function.</p>
<fieldset>
<legend>Demo</legend>
<div id="parentDivElement" style="border: 1px solid red; padding: 10px;">
     My name is <b>parentDivElement</b>.  I cannot be removed.</p>
<div id="childOne" style="border: 1px solid green; padding: 10px; margin: 10px;">
          My name is <b>childOne</b>.
     </div>
<div id="childTwo" style="border: 1px solid blue; padding: 10px; margin: 10px;">
          My name is <b>childTwo</b>.
     </div>
<div id="childThree" style="border: 1px solid purple; padding: 10px; margin: 10px;">
          My name is <b>childThree</b>.
     </div>
</div>
<p>&nbsp;</p>
<p><input id="cb1" type="checkbox" value="childOne" onclick="removeElement('parentDivElement', this.value);"><label for="cb1">Remove <b>childOne</b></label><br />
<input id="cb2" type="checkbox" value="childTwo" onclick="removeElement('parentDivElement', this.value);"><label for="cb2">Remove <b>childTwo</b></label><br />
<input id="cb3" type="checkbox" value="childThree" onclick="removeElement('parentDivElement', this.value);"><label for="cb3">Remove <b>childThree</b></label><br />
</fieldset>

<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
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;div id=&quot;parentDivElement&quot; style=&quot;border: 1px solid red; padding: 10px;&quot;&gt;
     My name is &lt;b&gt;parentDivElement&lt;/b&gt;.  I cannot be removed.
     &lt;div id=&quot;childOne&quot; style=&quot;border: 1px solid green; padding: 10px; margin: 10px;&quot;&gt;
          My name is &lt;b&gt;childOne&lt;/b&gt;.
     &lt;/div&gt;
     &lt;div id=&quot;childTwo&quot; style=&quot;border: 1px solid blue; padding: 10px; margin: 10px;&quot;&gt;
          My name is &lt;b&gt;childTwo&lt;/b&gt;.
     &lt;/div&gt;
     &lt;div id=&quot;childThree&quot; style=&quot;border: 1px solid purple; padding: 10px; margin: 10px;&quot;&gt;
          My name is &lt;b&gt;childThree&lt;/b&gt;.
     &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;input id=&quot;cb1&quot; type=&quot;checkbox&quot; value=&quot;childOne&quot; onClick=&quot;removeElement('parentDivElement', this.value);&quot;&gt;&lt;label for=&quot;cb1&quot;&gt;Remove &lt;b&gt;childOne&lt;/b&gt;&lt;/label&gt;
&lt;input id=&quot;cb2&quot; type=&quot;checkbox&quot; value=&quot;childTwo&quot; onClick=&quot;removeElement('parentDivElement', this.value);&quot;&gt;&lt;label for=&quot;cb2&quot;&gt;Remove &lt;b&gt;childTwo&lt;/b&gt;&lt;/label&gt;
&lt;input id=&quot;cb3&quot; type=&quot;checkbox&quot; value=&quot;childThree&quot; onClick=&quot;removeElement('parentDivElement', this.value);&quot;&gt;&lt;label for=&quot;cb3&quot;&gt;Remove &lt;b&gt;childThree&lt;/b&gt;&lt;/label&gt;</pre></td></tr></table></div>

<p>The concept is the same as the previous demos where we attach the <b>removeElement</b> function to an event handler, which in this case is the <b>onClick</b> event handler of the checkbox.  Once the checkbox is checked (or even unchecked), it fires off the <b>onClick</b> event handler and executes the <b>removeElement</b> JavaScript function and passes off the name of the parent div and the value of the checkbox.</p>
<p>If you are interested in hiding content via JavaScript and not necessarily deleting or removing them from the DOM, see my post on <a href="http://www.randomsnippets.com/2008/02/12/how-to-hide-and-show-your-div/" title="How to hide, show, or toggle your div">How to Hide and Show Your Div</a>.</p>
<p><span style="color: green;"><strong>Update</strong></span>: Since my initial writing of this blog post, some great JavaScript libraries have been implemented which makes removing elements <strong>much easier</strong>!  One of these JavaScript libraries is jQuery and I have done a similar post on removing elements from the DOM: <a href="http://www.randomsnippets.com/2011/08/21/how-to-dynamically-remove-elements-from-the-dom-via-jquery/" title="How to dynamically remove elements from the DOM via jQuery">How to dynamically remove elements from the DOM via jQuery</a>.</p>
<p>If you found that my code was helpful in any way, shape, or form and would like to buy me a <strong>beer</strong>, please use the <strong>Donate</strong> button below =)  Cheers!<br />
<center></p>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick"><br />
<input type="hidden" name="hosted_button_id" value="WB5GF5WQ6YE5U"><br />
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"><br />
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1"><br />
</form>
<p></center></p>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/03/26/how-to-dynamically-remove-delete-elements-via-javascript/feed/</wfw:commentRss>
		<slash:comments>30</slash:comments>
		</item>
		<item>
		<title>How to find and replace text dynamically via JavaScript</title>
		<link>http://www.randomsnippets.com/2008/03/07/how-to-find-and-replace-text-dynamically-via-javascript/</link>
		<comments>http://www.randomsnippets.com/2008/03/07/how-to-find-and-replace-text-dynamically-via-javascript/#comments</comments>
		<pubDate>Fri, 07 Mar 2008 07:18:20 +0000</pubDate>
		<dc:creator>Allen Liu</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[find]]></category>
		<category><![CDATA[find and replace]]></category>
		<category><![CDATA[findMyText]]></category>
		<category><![CDATA[haystack]]></category>
		<category><![CDATA[haystackText]]></category>
		<category><![CDATA[javascript function]]></category>
		<category><![CDATA[onclick event]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[replace]]></category>
		<category><![CDATA[Replacement]]></category>
		<category><![CDATA[replacement text]]></category>
		<category><![CDATA[text replacement]]></category>

		<guid isPermaLink="false">http://www.randomsnippets.com/2008/03/07/how-to-find-and-replace-text-dynamically-via-javascript/</guid>
		<description><![CDATA[This is a neat little script that demonstrates two things: 1) Find &#8211; If only the Find box is filled in, then the findMyText() JavaScript function will just perform a find for the div that is given. If there is &#8230; <a href="http://www.randomsnippets.com/2008/03/07/how-to-find-and-replace-text-dynamically-via-javascript/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p><script src="/wp-includes/js/findAndReplace.js" language="Javascript" type="text/javascript"></script><br />
This is a neat little script that demonstrates two things:</p>
<p>1) <b>Find</b> &#8211; If only the <b>Find</b> box is filled in, then the <b>findMyText()</b> JavaScript function will just perform a find for the div that is given.  If there is a match, the text will be highlighted and marked in bold.</p>
<p>2) <b>Find and Replace</b> &#8211; If both the <b>Find</b> and <b>Replace</b> boxes are filled in, then the <b>findMyText()</b> JavaScript function will do just that. </p>
<fieldset>
<legend>Demo</legend>
<div id="haystack">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
<p></p>
<table>
<tr>
<td>Find</td>
<td><input id="needle" name="needle" type="text"></td>
</tr>
<tr>
<td>Replacment</td>
<td><input id="replacement" name="replacement" type="text"></td>
</tr>
</table>
<p><input type="button" value="Find" onClick="findMyText(document.getElementById('needle').value, document.getElementById('replacement').value);"><br />
</fieldset>
<p><span id="more-11"></span><br />
Here is the plain HTML:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;div id=&quot;haystack&quot;&gt;
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
&lt;/div&gt;
&lt;br&gt;
&lt;table&gt;
&lt;tr&gt;&lt;td&gt;Find&lt;/td&gt;&lt;td&gt;&lt;input id=&quot;needle&quot; name=&quot;needle&quot; type=&quot;text&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Replacment&lt;/td&gt;&lt;td&gt;&lt;input id=&quot;replacement&quot; name=&quot;replacement&quot; type=&quot;text&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;input type=&quot;button&quot; value=&quot;Find&quot; onClick=&quot;findMyText(document.getElementById('needle').value, document.getElementById('replacement').value);&quot;&gt;</pre></td></tr></table></div>

<p>
Please excuse the use of the table for the text inputs and labels.  WordPress does some automatic formatting which is annoying sometimes but it is still a great application =)  The only thing of importance here is on line 9 where the <b>onClick</b> event executes the <b>findMyText()</b> function and passes 2 arguments:</p>
<ul>
<li><b>Needle</b> &#8211; This is the text that we are trying to find in the haystack of text.</li>
<li><b>Replacement</b> &#8211; This is our replacement text for any needle that we find.</li>
</ul>
<p>Here is our 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
12
13
14
15
16
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> haystackText <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">function</span> findMyText<span style="color: #009900;">&#40;</span>needle<span style="color: #339933;">,</span> replacement<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>haystackText.<span style="color: #660066;">length</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          haystackText <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;haystack&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
     <span style="color: #000066; font-weight: bold;">var</span> match <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> RegExp<span style="color: #009900;">&#40;</span>needle<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;ig&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>     
     <span style="color: #000066; font-weight: bold;">var</span> replaced <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
     <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>replacement.<span style="color: #660066;">length</span> <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          replaced <span style="color: #339933;">=</span> haystackText.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span>match<span style="color: #339933;">,</span> replacement<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
     <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000066; font-weight: bold;">var</span> boldText <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&lt;div style=<span style="color: #000099; font-weight: bold;">\&quot;</span>background-color: yellow; display: inline; font-weight: bold;<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span> <span style="color: #339933;">+</span> needle <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;&lt;/div&gt;&quot;</span><span style="color: #339933;">;</span>
          replaced <span style="color: #339933;">=</span> haystackText.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span>match<span style="color: #339933;">,</span> boldText<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
     document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;haystack&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> replaced<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Like many of the scripts that you see here, using the JavaScript DOM objects is the key to doing the find and replace dynamically for our website.  </p>
<p>The script asks for two parameters: the needle which we are looking for in our text and the text we want to replace it with.  Please note that the replacement text is totally <i>optional</i>.  If no replacement text is given, then the behavior of the function changes a bit.</p>
<p>The <b>haystackText</b> variable is outside of the function in line 1 because we want to store the original text and always base our searches on the original text.  In lines 3-5 is where we store the original text in our <b>haystack</b> div if it is not already set.</p>
<p>Line 6 creates a regular expression object where we place our <b>needle</b> variable along with two flags:</p>
<ul>
<li><b>i</b> &#8211; This flag causes the regular expression match to be case-insensitive.</li>
<li><b>g</b> &#8211; This flag causes the regular expression match to be global so it matches every occurrence instead of just the first one.</li>
</ul>
<p>Lines 8-10 checks to see if the <b>replacement</b> text was set.  If so, we will use the <b>replace</b> method on our <b>haystackText</b> string object and feed it our <b>match</b> and <b>replacement</b> texts.  The resulting string will be saved in the <b>replaced</b> variable.</p>
<p>If the <b>replacement</b> text was not set, the <b>else</b> block in lines 11-14 will be executed.  We first prepare a variable of our <b>needle</b> that has a bit of extra styling such as a yellow background and bold text so it stands out better.  Next, we just use the <b>replace</b> method on our <b>haystackText</b> string object and pass the <b>match</b> and <b>replacement</b> texts.  The resulting string will be saved in the <b>replaced</b> variable.  </p>
<p>Lastly, the <b>replaced</b> variable which contains our results is used to substitute what was originally in our <b>haystack</b> div.</p>
<p>If you found that my code was helpful in any way, shape, or form and would like to buy me a <strong>beer</strong>, please use the <strong>Donate</strong> button below =)  Cheers!</p>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick"><br />
<input type="hidden" name="hosted_button_id" value="P7VD7JXQGY2YJ"><br />
<input type="image" style="border: none;" src="https://www.paypalobjects.com/WEBSCR-640-20110306-1/en_US/i/btn/btn_donateCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"><br />
<img alt="" border="0" src="https://www.paypalobjects.com/WEBSCR-640-20110306-1/en_US/i/scr/pixel.gif" width="1" height="1"><br />
</form>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/03/07/how-to-find-and-replace-text-dynamically-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>Allen Liu</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. &#8230; <a href="http://www.randomsnippets.com/2008/03/05/simulate-a-button-click-via-javascript/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></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>
<p><input type="checkbox" onClick="document.getElementById('theSubmitButton').click();">Check the box to simulate a button click<br />
<br />
<input type="button" name="theSubmitButton" id="theSubmitButton" value="Button" onClick="alert('The button was clicked.');"><br />
</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" 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> name<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>
<p>If you found that my code was helpful in any way, shape, or form and would like to buy me a <strong>beer</strong>, please use the <strong>Donate</strong> button below =)  Cheers!</p>
<p><center></p>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick"><br />
<input type="hidden" name="hosted_button_id" value="WB5GF5WQ6YE5U"><br />
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"><br />
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1"><br />
</form>
<p></center></p>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/03/05/simulate-a-button-click-via-javascript/feed/</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
		<item>
		<title>JavaScript to select all or none of the checkboxes in a form</title>
		<link>http://www.randomsnippets.com/2008/02/28/javascript-to-select-all-or-none-of-the-checkboxes-in-a-form/</link>
		<comments>http://www.randomsnippets.com/2008/02/28/javascript-to-select-all-or-none-of-the-checkboxes-in-a-form/#comments</comments>
		<pubDate>Thu, 28 Feb 2008 07:24:09 +0000</pubDate>
		<dc:creator>Allen Liu</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[checkboxes]]></category>
		<category><![CDATA[demo]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[javascript function]]></category>
		<category><![CDATA[selectToggle]]></category>

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

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

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

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

<p>This function basically loops through all of the inputs in the given form name that is passed over as an argument.  Depending on the value of the <code>toggle</code> parameter, which can be <code>true</code> or <code>false</code>, each form element will checked or unchecked respectively.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/02/28/javascript-to-select-all-or-none-of-the-checkboxes-in-a-form/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Form verification via JavaScript</title>
		<link>http://www.randomsnippets.com/2008/02/27/form-verification-via-javascript/</link>
		<comments>http://www.randomsnippets.com/2008/02/27/form-verification-via-javascript/#comments</comments>
		<pubDate>Wed, 27 Feb 2008 07:27:12 +0000</pubDate>
		<dc:creator>Allen Liu</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[demo]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[email form]]></category>
		<category><![CDATA[error warnings]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[form email]]></category>
		<category><![CDATA[onSubmit]]></category>
		<category><![CDATA[verification]]></category>
		<category><![CDATA[verifyForm]]></category>

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

<div class="wp_syntax"><table><tr><td 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></td></tr></table></div>

<p>Notice that the error warnings such as the <b>Please fill in your username.</b> or <b>Please fill in your email.</b> have already been preloaded but hidden from the viewer by setting the <code>display</code> attribute to <code>none</code>.  If the user submits the form with one or more of the fields blank, 4 things will happen to notify the user (Note: I tried to squeeze in as many warning events as possible just for demonstration purposes so I apologize if it is a bit over the top):</p>
<ol>
<li>the background of the missing input field will turn yellow</li>
<li>a red border will appear around the missing input field</li>
<li>the hidden error message will become visible</li>
<li>an alert will pop up notifying the user that 1 or more fields are missing</li>
</ol>
<p>When the user clicks on the <b>Submit</b> button, the <b>onSubmit</b> event gets triggered and executes the <b>verifyForm()</b> JavaScript function.  If the <b>onSubmit</b> event returns a <code>false</code> value, the form does not get submitted.  This value is determined by the <b>verifyForm()</b> function, which returns <code>true</code> if all fields are filled in and returns <code>false</code> if one or more fields are missing.   </p>
<p>Here is a peek at the JavaScript <b>verifyForm()</b> function:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; 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: #000066; font-weight: bold;">var</span> userName <span style="color: #339933;">=</span> form.<span style="color: #660066;">name</span>.<span style="color: #660066;">value</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">var</span> userEmail <span style="color: #339933;">=</span> form.<span style="color: #660066;">email</span>.<span style="color: #660066;">value</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">var</span> success <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>userName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;usernameMsg&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;block&quot;</span><span style="color: #339933;">;</span>
        form.<span style="color: #660066;">name</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">backgroundColor</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;yellow&quot;</span><span style="color: #339933;">;</span>
        form.<span style="color: #660066;">name</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">border</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;3px red solid&quot;</span><span style="color: #339933;">;</span>
        success <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
        form.<span style="color: #660066;">name</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">backgroundColor</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
        form.<span style="color: #660066;">name</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">border</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
        document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;usernameMsg&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>userEmail<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;emailMsg&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;block&quot;</span><span style="color: #339933;">;</span>
        form.<span style="color: #660066;">email</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">backgroundColor</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;yellow&quot;</span><span style="color: #339933;">;</span>
        form.<span style="color: #660066;">email</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">border</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;3px red solid&quot;</span><span style="color: #339933;">;</span>
        success <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
        form.<span style="color: #660066;">email</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">backgroundColor</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
        form.<span style="color: #660066;">email</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">border</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
        document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;emailMsg&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>success<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        alert<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;The form is incomplete.  Please read the error message(s).&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
        alert<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;The form was submitted succesfully!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

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

		<guid isPermaLink="false">http://www.randomsnippets.com/2008/02/24/dynamically-edit-font-styling-of-html-content-via-javascript/</guid>
		<description><![CDATA[The Document Object Model (DOM) allows for dynamic styling because it makes all HTML elements and attributes readily accessible using JavaScript. Here is a simple demo of how powerful this technology can be: Demo Hello world! Here is the JavaScript &#8230; <a href="http://www.randomsnippets.com/2008/02/24/dynamically-edit-font-styling-of-html-content-via-javascript/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>The Document Object Model (DOM) allows for dynamic styling because it makes all HTML elements and attributes readily accessible using JavaScript.  Here is a simple demo of how powerful this technology can be:</p>
<fieldset>
<legend>Demo</legend>
<div id="dynamicContent">Hello world!</div>
<p>
<input type="button" onClick="document.getElementById('dynamicContent').style.fontWeight = 'bolder';" value="Bold"><br />
<input type="button" onClick="document.getElementById('dynamicContent').style.fontWeight = 'lighter';" value="Lighter"><br />
<input type="button" onClick="document.getElementById('dynamicContent').style.fontStyle = 'normal';" value="Normal"><br />
<input type="button" onClick="document.getElementById('dynamicContent').style.fontStyle = 'italic';" value="Italic"><br />
<input type="button" onClick="document.getElementById('dynamicContent').style.fontFamily = 'courier';" value="Courier"><br />
<input type="button" onClick="document.getElementById('dynamicContent').style.fontFamily = 'roman';" value="Times Roman"><br />
</fieldset>
<p><span id="more-7"></span><br />
Here is the JavaScript code that makes all this possible:<br />
</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;dynamicContent&quot;</span><span style="color: #339933;">&gt;</span>Hello world<span style="color: #339933;">!&lt;/</span>div<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> onClick<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;document.getElementById('dynamicContent').style.fontWeight = 'bolder';&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;Bold&quot;</span><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> onClick<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;document.getElementById('dynamicContent').style.fontWeight = 'lighter';&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;Lighter&quot;</span><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> onClick<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;document.getElementById('dynamicContent').style.fontStyle = 'normal';&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;Normal&quot;</span><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> onClick<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;document.getElementById('dynamicContent').style.fontStyle = 'italic';&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;Italic&quot;</span><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> onClick<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;document.getElementById('dynamicContent').style.fontFamily = 'courier';&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;Courier&quot;</span><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> onClick<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;document.getElementById('dynamicContent').style.fontFamily = 'roman';&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;Times Roman&quot;</span><span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>
In this example, we use the JavaScript <b>onClick</b> event handler to dynamically change the content styling of the text within the div tags.  To access the div object, we use the <b>getElementById()</b> function and pass over the div id which is, <b>dynamicContent</b> in this case, as the argument.  Once we have access to the div element, we have access to all the styles that are associated with it. </p>
<p>There are a myriad of things that you can change such as font size, font weight, font face, font color, background color, borders and much much more.<br /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/02/24/dynamically-edit-font-styling-of-html-content-via-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to dynamically add form elements via JavaScript</title>
		<link>http://www.randomsnippets.com/2008/02/21/how-to-dynamically-add-form-elements-via-javascript/</link>
		<comments>http://www.randomsnippets.com/2008/02/21/how-to-dynamically-add-form-elements-via-javascript/#comments</comments>
		<pubDate>Thu, 21 Feb 2008 08:00:43 +0000</pubDate>
		<dc:creator>Allen Liu</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[appendchild]]></category>
		<category><![CDATA[demo]]></category>
		<category><![CDATA[div element]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[dynamic text]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[input button]]></category>
		<category><![CDATA[input text]]></category>
		<category><![CDATA[javascript function]]></category>
		<category><![CDATA[text element]]></category>
		<category><![CDATA[text input]]></category>
		<category><![CDATA[text inputs]]></category>

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

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

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

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> counterText <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> counterRadioButton <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> counterCheckBox <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> counterTextArea <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
<span style="color: #000066; 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: #000066; font-weight: bold;">var</span> newdiv <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #000066; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span>inputType<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'text'</span><span style="color: #339933;">:</span>
               newdiv.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Entry &quot;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>counterText <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; &lt;br&gt;&lt;input type='text' name='myInputs[]'&gt;&quot;</span><span style="color: #339933;">;</span>
               counterText<span style="color: #339933;">++;</span>
               <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
          <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'radio'</span><span style="color: #339933;">:</span>
               newdiv.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Entry &quot;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>counterRadioButton <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; &lt;br&gt;&lt;input type='radio' name='myRadioButtons[]'&gt;&quot;</span><span style="color: #339933;">;</span>
               counterRadioButton<span style="color: #339933;">++;</span>
               <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
          <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'checkbox'</span><span style="color: #339933;">:</span>
               newdiv.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Entry &quot;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>counterCheckBox <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; &lt;br&gt;&lt;input type='checkbox' name='myCheckBoxes[]'&gt;&quot;</span><span style="color: #339933;">;</span>
               counterCheckBox<span style="color: #339933;">++;</span>
               <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
          <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'textarea'</span><span style="color: #339933;">:</span>
	       newdiv.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Entry &quot;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>counterTextArea <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; &lt;br&gt;&lt;textarea name='myTextAreas[]'&gt;type here...&lt;/textarea&gt;&quot;</span><span style="color: #339933;">;</span>
               counterTextArea<span style="color: #339933;">++;</span>
               <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
          <span style="color: #009900;">&#125;</span>
     document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>divName<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>newdiv<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>If you found that my code was helpful in any way, shape, or form and would like to buy me a <strong>beer</strong>, please use the <strong>Donate</strong> button below =)  Cheers!</p>
<p><center></p>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick"><br />
<input type="hidden" name="hosted_button_id" value="WB5GF5WQ6YE5U"><br />
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"><br />
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1"><br />
</form>
<p></center></p>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/02/21/how-to-dynamically-add-form-elements-via-javascript/feed/</wfw:commentRss>
		<slash:comments>109</slash:comments>
		</item>
		<item>
		<title>How to hide, show, or toggle your div</title>
		<link>http://www.randomsnippets.com/2008/02/12/how-to-hide-and-show-your-div/</link>
		<comments>http://www.randomsnippets.com/2008/02/12/how-to-hide-and-show-your-div/#comments</comments>
		<pubDate>Wed, 13 Feb 2008 06:45:00 +0000</pubDate>
		<dc:creator>Allen Liu</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[collapse]]></category>
		<category><![CDATA[demo show]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[Hide]]></category>
		<category><![CDATA[html javascript]]></category>
		<category><![CDATA[javascript code]]></category>
		<category><![CDATA[javascript function]]></category>
		<category><![CDATA[javascript functions]]></category>

		<guid isPermaLink="false">http://www.randomsnippets.com/2008/02/12/how-to-hide-and-show-your-div/</guid>
		<description><![CDATA[Are you trying to find a way to hide and show your content? The demo below shows a simple yet elegant way of toggling your content and toggling the control text via Javascript and styling. Demo Let's use images! click &#8230; <a href="http://www.randomsnippets.com/2008/02/12/how-to-hide-and-show-your-div/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Are you trying to find a way to hide and show your content?  The demo below shows a simple yet elegant way of toggling your content and toggling the control text via Javascript and styling.</p>
<p><script type="text/javascript"> 
function toggle() {
	var ele = document.getElementById("toggleText");
	var text = document.getElementById("displayText");
	if(ele.style.display == "block") {
    		ele.style.display = "none";
		text.innerHTML = "show";
  	}
	else {
		ele.style.display = "block";
		text.innerHTML = "hide";
	}
} 
function toggle2(showHideDiv, switchTextDiv) {
	var ele = document.getElementById(showHideDiv);
	var text = document.getElementById(switchTextDiv);
	if(ele.style.display == "block") {
    		ele.style.display = "none";
		text.innerHTML = "restore";
  	}
	else {
		ele.style.display = "block";
		text.innerHTML = "collapse";
	}
} 
function toggle3(contentDiv, controlDiv) {
        if (contentDiv.constructor == Array) {
                for(i=0; i < contentDiv.length; i++) {
                     toggle2(contentDiv[i], controlDiv[i]);
                }
        }
        else {
               toggle2(contentDiv, controlDiv);
        }
} 
var counter = 0;
var numBoxes = 3;
function toggle4(showHideDiv) {
	var ele = document.getElementById(showHideDiv + counter);
	if(ele.style.display == "block") {
    		ele.style.display = "none";
  	}
	else {
		ele.style.display = "block";
       }
       if(counter == numBoxes) {
                document.getElementById("toggleButton").style.display = "none";
       }
} 
function showonlyone(thechosenone) {
      var newboxes = document.getElementsByTagName("div");
            for(var x=0; x<newboxes.length; x++) {
                  name = newboxes[x].getAttribute('class');
                  if (name == 'newboxes') {
                        if (newboxes[x].id == thechosenone) {
                        newboxes[x].style.display = 'block';
                  }
                  else {
                        newboxes[x].style.display = 'none';
                  }
            }
      }
}
function showonlyonev2(thechosenone) {
      var newboxes = document.getElementsByTagName("div");
      for(var x=0; x<newboxes.length; x++) {
            name = newboxes[x].getAttribute('class');
            if (name == 'newboxes-2') {
                  if (newboxes[x].id == thechosenone) {
                        if (newboxes[x].style.display == 'block') {
                              newboxes[x].style.display = 'none';
                        }
                        else {
                              newboxes[x].style.display = 'block';
                        }
                  }else {
                        newboxes[x].style.display = 'none';
                  }
            }
      }
}
</script></p>
<style type="text/css">
#headerDivImg, #contentDivImg, #contentDivImg_ {
     float: left;
     width: 510px;
}
#titleTextImg {
     float: left;
     font-size: 1.2em;
     font-weight: bold;
     margin: 5px;
}
#imageDivLink {
     float: right;
}
#headerDivImg {
     background-color: #0037DB;
     color: #9EB6FF;
}
#contentDivImg, #contentDivImg_ {
     background-color: #FFE694;
     text-align: center;
}
#headerDivImg img {
     float: right;
     margin: 10px 10px 5px 5px;
}
</style>
<p><script src="/wp-includes/js/showhidediv.js" language="Javascript" type="text/javascript"></script></p>
<fieldset>
<legend>Demo</legend>
<div id="headerDivImg">
<div id="titleTextImg">Let's use images!  click image to expand/collapse==></div>
<p>    <a id="imageDivLink" href="javascript:toggle5('contentDivImg', 'imageDivLink');"><img src="/wp-includes/images/minus.png"></a>
</div>
<div id="contentDivImg" style="display: block;">This demo uses plus and minus images for hiding and showing your div dynamically via JavaScript. This content will toggle.</div>
</fieldset>
<p>We will cover how to implement this example later on in the tutorial but first let's start with a simple example.</p>
<fieldset>
<legend>Demo</legend>
<p><a id="displayText" href="javascript:toggle();" >show</a> <== click here</p>
<div id="toggleText" style="display:none">
<h1>peek-a-boo</h1>
</div>
</fieldset>
<p><span id="more-5"></span></p>
<p>Here is the sample HTML and Javascript code:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script language<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;javascript&quot;</span><span style="color: #339933;">&gt;</span> 
<span style="color: #000066; font-weight: bold;">function</span> toggle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">var</span> ele <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;toggleText&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">var</span> text <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;displayText&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>ele.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;block&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    		ele.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #339933;">;</span>
		text.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;show&quot;</span><span style="color: #339933;">;</span>
  	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
		ele.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;block&quot;</span><span style="color: #339933;">;</span>
		text.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;hide&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span> 
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
&nbsp;
&lt;a id=&quot;displayText&quot; href=&quot;javascript:toggle();&quot;&gt;show&lt;/a&gt; &lt;== click Here
&lt;div id=&quot;toggleText&quot; style=&quot;display: none&quot;&gt;&lt;h1&gt;peek-a-boo&lt;/h1&gt;&lt;/div&gt;</pre></td></tr></table></div>

<p>By default, the <code>peek-a-boo</code> text is loaded when the page loads but the <code>display</code> attribute for the div that the content resides in is set to <code>none</code> so it is not visible to the visitor.  When the link is clicked, the <code>toggle()</code> JavaScript functions executes and checks the value of the display style for the div that contains the content that we want to toggle.</p>
<p><b>If the display style is <code>none</code></b>, the function will:</p>
<ul>
<li>Set the display style to <code>block</code> - This is executed in the <code>else</code> block of the function.  The inner HTML content of a DOM element with a <code>block</code> display setting will be visible unless it is furthered controlled by CSS styling.</li>
<li>Change the link text to <code>hide</code> - The inner HTML of the link text, which in this case is just <code>show</code>, is replaced with the <code>hide</code> text.</li>
</ul>
<p><b>If the display style is <code>block</code></b>, the function will:</p>
<ul>
<li>Set the display style to <code>none</code> - This is executed in the <code>if</code> block of the function.  The inner HTML content of a DOM element with the <code>none</code> display setting will not be visible for the viewer.</li>
<li>Change the link text to <code>show</code> - The inner HTML of the link text, which in this case is just <code>hide</code>, is replaced with the <code>show</code> text.</li>
</ul>
<hr />
<p>Here is a more reusable and flexible <code>toggle</code> function that takes 2 parameters: one for the div to hide/show and a second parameter for the div that contains the link text to be switched.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script language<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;javascript&quot;</span><span style="color: #339933;">&gt;</span> 
<span style="color: #000066; font-weight: bold;">function</span> toggle<span style="color: #009900;">&#40;</span>showHideDiv<span style="color: #339933;">,</span> switchTextDiv<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">var</span> ele <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>showHideDiv<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">var</span> text <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>switchTextDiv<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>ele.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;block&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    		ele.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #339933;">;</span>
		text.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;show&quot;</span><span style="color: #339933;">;</span>
  	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
		ele.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;block&quot;</span><span style="color: #339933;">;</span>
		text.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;hide&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span> 
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<style type="text/css">
#headerDiv, #contentDiv {
     float: left;
     width: 510px;
}
#titleText {
     float: left;
     font-size: 1.1em;
     font-weight: bold;
     margin: 5px;
}
#myHeader {
     font-size: 1.1em;
     font-weight: bold;
     margin: 5px;
}
#headerDiv {
     background-color: #0037DB;
     color: #9EB6FF;
}
#contentDiv {
     background-color: #FFE694;
}
#myContent {
     margin: 0px 10px;
}
#headerDiv a {
     float: right;
}
#headerDiv a:hover {
     color: #FFFFFF;
}
</style>
<p>If you spice up this demo with some extra CSS styling, this can look like a nice little dialog box.</p>
<fieldset>
<legend>Demo</legend>
<div id="headerDiv">
<div id="titleText">Hide/Show Div Demo - Click here ==></div>
<p><a id="myHeader" href="javascript:toggle2('myContent','myHeader');" >collapse</a>
   </div>
<div style="clear:both;"></div>
<div id="contentDiv">
<div id="myContent" style="display: block;">
<p>This example demonstrates how CSS styling can make this look like a window that you can <b>collapse</b> and <b>restore</b>.  How cool is that?!</p>
<p>Here is what the CSS styling looks like:</p>
<p><code><br />
#headerDiv, #contentDiv {<br />
     float: left;<br />
     width: 510px;<br />
}<br />
#titleText {<br />
     float: left;<br />
     font-size: 1.1em;<br />
     font-weight: bold;<br />
     margin: 5px;<br />
}<br />
#myHeader {<br />
     font-size: 1.1em;<br />
     font-weight: bold;<br />
     margin: 5px;<br />
}<br />
#headerDiv {<br />
     background-color: #0037DB;<br />
     color: #9EB6FF;<br />
}<br />
#contentDiv {<br />
     background-color: #FFE694;<br />
}<br />
#myContent {<br />
     margin: 5px 10px;<br />
}<br />
#headerDiv a {<br />
     float: right;<br />
     margin: 10px 10px 5px 5px;<br />
}<br />
#headerDiv a:hover {<br />
     color: #FFFFFF;<br />
}<br />
</code></p>
<p>Here is the HTML code:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="html" style="font-family:monospace;">&lt;div id=&quot;headerDiv&quot;&gt;
     &lt;div id=&quot;titleText&quot;&gt;Hide/Show Div Demo - Click here ==&gt;&lt;/div&gt;&lt;a id=&quot;myHeader&quot; href=&quot;javascript:toggle2('myContent','myHeader');&quot; &gt;collapse&lt;/a&gt;
&lt;/div&gt;
&lt;div style=&quot;clear:both;&quot;&gt;&lt;/div&gt;
&lt;div id=&quot;contentDiv&quot;&gt;
     &lt;div id=&quot;myContent&quot; style=&quot;display: block;&quot;&gt;This is the content that is dynamically being collapsed.&lt;/div&gt;
&lt;/div&gt;</pre></td></tr></table></div>

<p>That's all there is to it! =)
</p></div>
</div>
</fieldset>
<p>Here is the <strong>toogle2</strong> JavaScript function:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> toggle2<span style="color: #009900;">&#40;</span>showHideDiv<span style="color: #339933;">,</span> switchTextDiv<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">var</span> ele <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>showHideDiv<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">var</span> text <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>switchTextDiv<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>ele.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;block&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    		ele.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #339933;">;</span>
		text.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;restore&quot;</span><span style="color: #339933;">;</span>
  	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
		ele.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;block&quot;</span><span style="color: #339933;">;</span>
		text.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;collapse&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<hr />
<p>As requested, here is an example of a JavaScript function that toggles multiple elements simultaneously.  You can either toggle each DIV individually or use the button to toggle all 3 regardless of which toggle mode they are in.</p>
<fieldset>
<legend>Demo</legend>
<table>
<tr>
<td>
<div style="border: 1px solid blue; background-color: #99CCFF; padding: 5px; width: 150px;">
            <a id="myHeader1" href="javascript:toggle2('myContent1','myHeader1');" >collapse</a>
         </div>
<div id="myContent1" style="border: 1px solid black; background-color: #CCCCCC; display: block;padding: 5px; width: 150px;">Div #1</div>
</td>
<td>
<div style="border: 1px solid blue; background-color: #99CCFF; padding: 5px; width: 150px;">
            <a id="myHeader2" href="javascript:toggle2('myContent2','myHeader2');" >collapse</a>
         </div>
<div id="myContent2" style="border: 1px solid black; background-color: #CCCCCC; display: block;padding: 5px; width: 150px;">Div #2</div
      </td>
<td>
<div style="border: 1px solid blue; background-color: #99CCFF; padding: 5px; width: 150px;">
            <a id="myHeader3" href="javascript:toggle2('myContent3', 'myHeader3');" >collapse</a>
         </div>
<div id="myContent3" style="border: 1px solid black; background-color: #CCCCCC; display: block;padding: 5px; width: 150px;">Div #3</div
      </td>
</tr>
</table>
<p><input type="button" value="Press me to toggle all 3 DIVs" onClick="toggle3(['myContent1', 'myContent2', 'myContent3'], ['myHeader1', 'myHeader2', 'myHeader3']);"><br />
</fieldset>
<p>This demo uses the <b>toggle2</b> function as previously demonstrated and a new function called <b>toggle3</b>.  I apologize for not being very creative on the function names.  Anyway, here is the JavaScript code for <b>toggle3</b>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> toggle3<span style="color: #009900;">&#40;</span>contentDiv<span style="color: #339933;">,</span> controlDiv<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>contentDiv.<span style="color: #660066;">constructor</span> <span style="color: #339933;">==</span> <span style="">Array</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> contentDiv.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                     toggle2<span style="color: #009900;">&#40;</span>contentDiv<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> controlDiv<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
               toggle2<span style="color: #009900;">&#40;</span>contentDiv<span style="color: #339933;">,</span> controlDiv<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Line 2 of the function checks to see if the first argument is an array or not.  If it is an array, it will also assume that the second argument is an array as well.  If it is an array, the script will loop through each element and execute <b>toggle2</b> with each pair of elements in the arrays.  Please note that this function also assumes that both arrays are in the same order such that <b>contentDiv[3]</b> and <b>controlDiv[3]</b> are a pair that refer to the same toggle element. </p>
<p>If the first argument is not an array, we will just pass the arguments as is to <b>toggle2</b>.</p>
<p>Here is the HTML code for the demo:</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
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;table&gt;
   &lt;tr&gt;
      &lt;td&gt;
         &lt;div style=&quot;border: 1px solid blue; background-color: #99CCFF; padding: 5px; width: 150px;&quot;&gt;
            &lt;a id=&quot;myHeader1&quot; href=&quot;javascript:toggle2('myContent1','myHeader1');&quot; &gt;collapse&lt;/a&gt;
         &lt;/div&gt;
         &lt;div id=&quot;myContent1&quot; style=&quot;border: 1px solid black; background-color: #CCCCCC; display: block;padding: 5px; width: 150px;&quot;&gt;Div #1&lt;/div&gt;
      &lt;/td&gt;
      &lt;td&gt;
         &lt;div style=&quot;border: 1px solid blue; background-color: #99CCFF; padding: 5px; width: 150px;&quot;&gt;
            &lt;a id=&quot;myHeader2&quot; href=&quot;javascript:toggle2('myContent2','myHeader2');&quot; &gt;collapse&lt;/a&gt;
         &lt;/div&gt;
         &lt;div id=&quot;myContent2&quot; style=&quot;border: 1px solid black; background-color: #CCCCCC; display: block;padding: 5px; width: 150px;&quot;&gt;Div #2&lt;/div
      &lt;/td&gt;
      &lt;td&gt;
         &lt;div style=&quot;border: 1px solid blue; background-color: #99CCFF; padding: 5px; width: 150px;&quot;&gt;
            &lt;a id=&quot;myHeader3&quot; href=&quot;javascript:toggle2('myContent3', 'myHeader3');&quot; &gt;collapse&lt;/a&gt;
         &lt;/div&gt;
         &lt;div id=&quot;myContent3&quot; style=&quot;border: 1px solid black; background-color: #CCCCCC; display: block;padding: 5px; width: 150px;&quot;&gt;Div #3&lt;/div
      &lt;/td&gt;
   &lt;/tr&gt;
&lt;/table&gt;
&lt;input type=&quot;button&quot; value=&quot;Press me to toggle all 3 DIVs&quot; onClick=&quot;toggle3(['myContent1', 'myContent2', 'myContent3'], ['myHeader1', 'myHeader2', 'myHeader3']);&quot;&gt;</pre></td></tr></table></div>

<p>All the excitement is jammed into line 23 where we call the <b>toggle3</b> function and pass over 2 arrays: one array containing all the content div ids and another array containing the header div ids.  The rest is history =)</p>
<hr />
<p>This demo was written in response to a request.  We start off with some hidden divs and each click of the button will reveal one div at a time.  When we have revealed all the divs, the button will disappear.</p>
<fieldset>
<legend>Demo</legend>
<table>
<tr>
<td style="width: 150px;">
<div id="box1" style="border: 1px solid black; background-color: #CCCCCC; display: none;padding: 5px;">First</div>
</td>
<td style="width: 150px;">
<div id="box2" style="border: 1px solid black; background-color: #CCCCCC; display: none;padding: 5px;">Second</div
      </td>
<td style="width: 150px;">
<div id="box3" style="border: 1px solid black; background-color: #CCCCCC; display: none;padding: 5px;">Third</div
      </td>
</tr>
</table>
<p><input id="toggleButton" type="button" value="Show me the money!" onclick="counter++; toggle4('box');"><br />
</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
10
11
12
13
14
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;table&gt;
   &lt;tr&gt;
      &lt;td style=&quot;width: 150px;&quot;&gt;
         &lt;div id=&quot;box1&quot; style=&quot;border: 1px solid black; background-color: #CCCCCC; display: none;padding: 5px;&quot;&gt;First&lt;/div&gt;
      &lt;/td&gt;
      &lt;td style=&quot;width: 150px;&quot;&gt;
         &lt;div id=&quot;box2&quot; style=&quot;border: 1px solid black; background-color: #CCCCCC; display: none;padding: 5px;&quot;&gt;Second&lt;/div
      &lt;/td&gt;
      &lt;td style=&quot;width: 150px;&quot;&gt;
         &lt;div id=&quot;box3&quot; style=&quot;border: 1px solid black; background-color: #CCCCCC; display: none;padding: 5px;&quot;&gt;Third&lt;/div
      &lt;/td&gt;
   &lt;/tr&gt;
&lt;/table&gt;
&lt;input id=&quot;toggleButton&quot; type=&quot;button&quot; value=&quot;Show me the money!&quot; onclick=&quot;counter++; toggle4('box');&quot;&gt;</pre></td></tr></table></div>

<p>The HTML code contains 3 hidden divs to start off with.  The button will launch the <b>toggle4</b> JavaScript function and pass over the prefix of the div IDs.  Each div id is named with the prefix <b>box</b> and a number following the name.  For example, <b>box1</b>, <b>box2</b>, and <b>box3</b>.  This is important for our JavaScript function.  In addition, it increments the <b>counter</b> by 1 each time.  This variable is initialized in our function.  </p>
<p>Here is the JavaScript code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> counter <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> numBoxes <span style="color: #339933;">=</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">function</span> toggle4<span style="color: #009900;">&#40;</span>showHideDiv<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
       <span style="color: #000066; font-weight: bold;">var</span> ele <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>showHideDiv <span style="color: #339933;">+</span> counter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
       <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>ele.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;block&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
              ele.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #339933;">;</span>
       <span style="color: #009900;">&#125;</span>
       <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
              ele.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;block&quot;</span><span style="color: #339933;">;</span>
       <span style="color: #009900;">&#125;</span>
       <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>counter <span style="color: #339933;">==</span> numBoxes<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;toggleButton&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #339933;">;</span>
       <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Lines 1 and 2 will initialize two very important variables for us:</p>
<ol>
<li><b>counter</b> - This variable will help us determine which box we will need to toggle.</li>
<li><b>numBoxes</b> - This variable represents the total number of boxes.  This is important for us to know when we should hide the button.</li>
</ol>
<p>Line 4 accesses the div we will need to toggle based on the name that is passed over as the argument and the counter.  When these 2 values are concatenated, we get the name of the div we will need to toggle.  </p>
<p>Lines 5-10 tells the same old story as before for toggling the content.</p>
<p>Lines 11-13 tests to see if we have reached our maximum number of divs to toggle.  If so, it will access the toggle button and set the <b>display</b> attribute to <b>none</b>.</p>
<hr />
<p>By popular demand, here is a demo that uses <b>images</b> instead of the Expand/Collapse text.</p>
<fieldset>
<legend>Demo</legend>
<div id="headerDivImg">
<div id="titleTextImg">Let's use images!</div>
<p>    <a id="imageDivLink_" href="javascript:toggle5('contentDivImg_', 'imageDivLink_');"><img src="/wp-includes/images/minus.png"></a>
</div>
<div id="contentDivImg_" style="display: block;">This demo uses plus and minus images for hiding and showing your div dynamically via JavaScript.</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
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;div id=&quot;headerDivImg&quot;&gt;
    &lt;div id=&quot;titleTextImg&quot;&gt;Let's use images!&lt;/div&gt;
    &lt;a id=&quot;imageDivLink&quot; href=&quot;javascript:toggle5('contentDivImg', 'imageDivLink');&quot;&gt;&lt;img src=&quot;/wp-includes/images/minus.png&quot;&gt;&lt;/a&gt;
&lt;/div&gt;
&lt;div id=&quot;contentDivImg&quot; style=&quot;display: block;&quot;&gt;This demo uses plus and minus images for hiding and showing your div dynamically via JavaScript.&lt;/div&gt;</pre></td></tr></table></div>

<p>Everything is pretty much the same as before except the image tag is used instead of the Expand/Collapse text.</p>
<p>Here is the JavaScript code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> toggle5<span style="color: #009900;">&#40;</span>showHideDiv<span style="color: #339933;">,</span> switchImgTag<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">var</span> ele <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>showHideDiv<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">var</span> imageEle <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>switchImgTag<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>ele.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;block&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                ele.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #339933;">;</span>
		imageEle.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;img src=&quot;/wp-includes/images/plus.png&quot;&gt;'</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                ele.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;block&quot;</span><span style="color: #339933;">;</span>
                imageEle.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;img src=&quot;/wp-includes/images/minus.png&quot;&gt;'</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The <b>toggle5</b> JavaScript function is pretty much the same as the rest of the toggle functions except that it switches the <b>img</b> tags instead of text.</p>
<hr />
<p>Here is a new demo in response to a request where only one div is displayed at any one time.</p>
<fieldset>
<legend>Demo</legend>
<table>
<tr>
<td>
<div style="border: 1px solid blue; background-color: #99CCFF; padding: 5px;">
            <a id="myHeader1" href="javascript:showonlyone('newboxes1');" >collapse</a>
         </div>
<div class="newboxes" id="newboxes1" style="border: 1px solid black; background-color: #CCCCCC; display: block;padding: 5px;">Div #1</div>
</td>
<td>
<div style="border: 1px solid blue; background-color: #99CCFF; padding: 5px;">
            <a id="myHeader2" href="javascript:showonlyone('newboxes2');" >collapse</a>
         </div>
<div class="newboxes" id="newboxes2" style="border: 1px solid black; background-color: #CCCCCC; display: none;padding: 5px;">Div #2</div>
</td>
<td>
<div style="border: 1px solid blue; background-color: #99CCFF; padding: 5px;">
            <a id="myHeader3" href="javascript:showonlyone('newboxes3');" >collapse</a>
         </div>
<div class="newboxes" id="newboxes3" style="border: 1px solid black; background-color: #CCCCCC; display: none;padding: 5px;">Div #3</div>
</td>
</tr>
</table>
</fieldset>
<p>Here is the plain HTML code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;table&gt;
   &lt;tr&gt;
      &lt;td&gt;
         &lt;div style=&quot;border: 1px solid blue; background-color: #99CCFF; padding: 5px;&quot;&gt;
            &lt;a id=&quot;myHeader1&quot; href=&quot;javascript:showonlyone('newboxes1');&quot; &gt;collapse&lt;/a&gt;
         &lt;/div&gt;
         &lt;div class=&quot;newboxes&quot; id=&quot;newboxes1&quot; style=&quot;border: 1px solid black; background-color: #CCCCCC; display: block;padding: 5px;&quot;&gt;Div #1&lt;/div&gt;
      &lt;/td&gt;
      &lt;td&gt;
         &lt;div style=&quot;border: 1px solid blue; background-color: #99CCFF; padding: 5px;&quot;&gt;
            &lt;a id=&quot;myHeader2&quot; href=&quot;javascript:showonlyone('newboxes2');&quot; &gt;collapse&lt;/a&gt;
         &lt;/div&gt;
         &lt;div class=&quot;newboxes&quot; id=&quot;newboxes2&quot; style=&quot;border: 1px solid black; background-color: #CCCCCC; display: none;padding: 5px;&quot;&gt;Div #2&lt;/div&gt;
      &lt;/td&gt;
      &lt;td&gt;
         &lt;div style=&quot;border: 1px solid blue; background-color: #99CCFF; padding: 5px;&quot;&gt;
            &lt;a id=&quot;myHeader3&quot; href=&quot;javascript:showonlyone('newboxes3');&quot; &gt;collapse&lt;/a&gt;
         &lt;/div&gt;
         &lt;div class=&quot;newboxes&quot; id=&quot;newboxes3&quot; style=&quot;border: 1px solid black; background-color: #CCCCCC; display: none;padding: 5px;&quot;&gt;Div #3&lt;/div&gt;
      &lt;/td&gt;
   &lt;/tr&gt;
&lt;/table&gt;</pre></td></tr></table></div>

<p>Clicking on the links will execute the <b>showonlyone</b> JavaScript function and pass on the name of the div id.</p>
<p>Here is the <b>showonlyone</b> JavaScript code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> showonlyone<span style="color: #009900;">&#40;</span>thechosenone<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">var</span> newboxes <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">var</span> x<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> x<span style="color: #339933;">&lt;</span>newboxes.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> x<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                  name <span style="color: #339933;">=</span> newboxes<span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;class&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>name <span style="color: #339933;">==</span> <span style="color: #3366CC;">'newboxes'</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>newboxes<span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">id</span> <span style="color: #339933;">==</span> thechosenone<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        newboxes<span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'block'</span><span style="color: #339933;">;</span>
                  <span style="color: #009900;">&#125;</span>
                  <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                        newboxes<span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'none'</span><span style="color: #339933;">;</span>
                  <span style="color: #009900;">&#125;</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>Thanks to Justin and Ulysses for helping out with the IE6 bug that was there =)<br />
Also, thanks to Mark for suggesting to switch from name attribute to using class attribute to be compliant with W3C standards =)</p>
<p>Line 2 will find all our divs with the <b>newboxes</b> name attribute and place them in an array.  Lines 3-10 will loop through these divs and check to see if the id of the div matches the id that was passed over to the function.  If there is a match, the function will set the display attribute to <b>block</b> which will make the div and all its contents visible.  If the id does not match, the display attribute will be set to none which will make the div and all its contents hidden.</p>
<hr />
<p>Here is a new demo in response to a request where only one div is displayed at any one time but also have the link to be able to hide the div as well (or basically toggle).</p>
<fieldset>
<legend>Demo</legend>
<table>
<tr>
<td>
<div style="border: 1px solid blue; background-color: #99CCFF; padding: 5px;">
            <a id="myHeader1-2" href="javascript:showonlyonev2('newboxes1-2');" >toggle</a>
         </div>
<div class="newboxes-2" id="newboxes1-2" style="border: 1px solid black; background-color: #CCCCCC; display: block;padding: 5px;">Div #1</div>
</td>
<td>
<div style="border: 1px solid blue; background-color: #99CCFF; padding: 5px;">
            <a id="myHeader2-2" href="javascript:showonlyonev2('newboxes2-2');" >toggle</a>
         </div>
<div class="newboxes-2" id="newboxes2-2" style="border: 1px solid black; background-color: #CCCCCC; display: none;padding: 5px;">Div #2</div>
</td>
<td>
<div style="border: 1px solid blue; background-color: #99CCFF; padding: 5px;">
            <a id="myHeader3-2" href="javascript:showonlyonev2('newboxes3-2');" >toggle</a>
         </div>
<div class="newboxes-2" id="newboxes3-2" style="border: 1px solid black; background-color: #CCCCCC; display: none;padding: 5px;">Div #3</div>
</td>
</tr>
</table>
</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
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;table&gt;
   &lt;tr&gt;
      &lt;td&gt;
         &lt;div style=&quot;border: 1px solid blue; background-color: #99CCFF; padding: 5px;&quot;&gt;
            &lt;a id=&quot;myHeader1-2&quot; href=&quot;javascript:showonlyonev2('newboxes1-2');&quot; &gt;toggle&lt;/a&gt;
         &lt;/div&gt;
         &lt;div class=&quot;newboxes-2&quot; id=&quot;newboxes1-2&quot; style=&quot;border: 1px solid black; background-color: #CCCCCC; display: block;padding: 5px;&quot;&gt;Div #1&lt;/div&gt;
      &lt;/td&gt;
      &lt;td&gt;
         &lt;div style=&quot;border: 1px solid blue; background-color: #99CCFF; padding: 5px;&quot;&gt;
            &lt;a id=&quot;myHeader2-2&quot; href=&quot;javascript:showonlyonev2('newboxes2-2');&quot; &gt;toggle&lt;/a&gt;
         &lt;/div&gt;
         &lt;div class=&quot;newboxes-2&quot; id=&quot;newboxes2-2&quot; style=&quot;border: 1px solid black; background-color: #CCCCCC; display: none;padding: 5px;&quot;&gt;Div #2&lt;/div&gt;
      &lt;/td&gt;
      &lt;td&gt;
         &lt;div style=&quot;border: 1px solid blue; background-color: #99CCFF; padding: 5px;&quot;&gt;
            &lt;a id=&quot;myHeader3-2&quot; href=&quot;javascript:showonlyonev2('newboxes3-2');&quot; &gt;toggle&lt;/a&gt;
         &lt;/div&gt;
         &lt;div class=&quot;newboxes-2&quot; id=&quot;newboxes3-2&quot; style=&quot;border: 1px solid black; background-color: #CCCCCC; display: none;padding: 5px;&quot;&gt;Div #3&lt;/div&gt;
      &lt;/td&gt;
   &lt;/tr&gt;
&lt;/table&gt;</pre></td></tr></table></div>

<p>Here is the 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
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> showonlyonev2<span style="color: #009900;">&#40;</span>thechosenone<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">var</span> newboxes <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">var</span> x<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> x<span style="color: #339933;">&lt;</span>newboxes.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> x<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            name <span style="color: #339933;">=</span> newboxes<span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;class&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>name <span style="color: #339933;">==</span> <span style="color: #3366CC;">'newboxes-2'</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>newboxes<span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">id</span> <span style="color: #339933;">==</span> thechosenone<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>newboxes<span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'block'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                              newboxes<span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'none'</span><span style="color: #339933;">;</span>
                        <span style="color: #009900;">&#125;</span>
                        <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                              newboxes<span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'block'</span><span style="color: #339933;">;</span>
                        <span style="color: #009900;">&#125;</span>
                  <span style="color: #009900;">&#125;</span><span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                        newboxes<span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'none'</span><span style="color: #339933;">;</span>
                  <span style="color: #009900;">&#125;</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 is very similar to the <code>showonlyone()</code> function except now we are toggling the content.</p>
<p>Here is a tutorial that shows <a href="http://www.randomsnippets.com/2011/10/09/how-to-hide-or-show-content-based-on-links-or-urls-via-javascript-jquery/">how to hide or show content based on links on different pages</a>.</p>
<p>If there any other demos/tutorials that you would like to see on this subject, please feel free to comment below and I will try my best to implement it.</p>
<p><strong>Note:</strong> If you have an issue with your code, please give me a URL to work with.  It's extremely difficult for me to help if I cannot actually see the code.</p>
<p><span style="color: green; font-weight: bold;">UPDATE</span>: Since this post has been pretty popular, I felt is was my duty to update it newer methods that was not available at the time of writing this blog.  Nowadays, it is much easier to use <a href="http://www.randomsnippets.com/2011/04/10/how-to-hide-show-or-toggle-your-div-with-jquery/">jQuery to hide or show content</a>.  </p>
<p>If you found that my code was helpful in any way, shape, or form and would like to buy me a <strong>beer</strong>, please use the <strong>Donate</strong> button below =)  Cheers!  <strong>OR</strong>...you can sign up for a <a href="http://db.tt/aYkS0ZA">free Dropbox account using my referral link</a> =)  If you are not aware of what Dropbox is, it is the most awesomest online backup application there and so much more...all for <strong>free</strong>.</p>
<p><center></p>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick"><br />
<input type="hidden" name="hosted_button_id" value="WB5GF5WQ6YE5U"><br />
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"><br />
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1"><br />
</form>
<p></center></p>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/02/12/how-to-hide-and-show-your-div/feed/</wfw:commentRss>
		<slash:comments>449</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>Allen Liu</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 &#8230; <a href="http://www.randomsnippets.com/2008/02/09/dynamic-country-state-javascript-menu-example/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></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>
<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"><table><tr><td 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></td></tr></table></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"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; 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: #000066; font-weight: bold;">var</span> stateSelect <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</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: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>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: #339933;">;</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: #339933;">;</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<span style="color: #339933;">;</span>
	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><span style="color: #339933;">;</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: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></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>11</slash:comments>
		</item>
	</channel>
</rss>
