<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Random Snippets &#187; javascript functions</title>
	<atom:link href="http://www.randomsnippets.com/tag/javascript-functions/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.randomsnippets.com</link>
	<description>Random Snippets of Code and Ideas for any Web Developer</description>
	<lastBuildDate>Wed, 05 Aug 2009 14:32:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to dynamically add content to a div and store the content to a cookie via JavaScript</title>
		<link>http://www.randomsnippets.com/2008/04/14/how-to-dynamically-add-content-to-a-div-via-javascript/</link>
		<comments>http://www.randomsnippets.com/2008/04/14/how-to-dynamically-add-content-to-a-div-via-javascript/#comments</comments>
		<pubDate>Tue, 15 Apr 2008 06:45:17 +0000</pubDate>
		<dc:creator>Knix</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[add]]></category>
		<category><![CDATA[content]]></category>
		<category><![CDATA[cookieContent]]></category>
		<category><![CDATA[demo]]></category>
		<category><![CDATA[demo content]]></category>
		<category><![CDATA[div id]]></category>
		<category><![CDATA[document cookie]]></category>
		<category><![CDATA[document getelementbyid]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[dynamic content]]></category>
		<category><![CDATA[Dynamically]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[getCookie]]></category>
		<category><![CDATA[getElementById]]></category>
		<category><![CDATA[input text]]></category>
		<category><![CDATA[javascript code]]></category>
		<category><![CDATA[javascript function]]></category>
		<category><![CDATA[javascript functions]]></category>
		<category><![CDATA[onClick]]></category>
		<category><![CDATA[setCookie]]></category>
		<category><![CDATA[store content]]></category>

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

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

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

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

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

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

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

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

<p>Please refer to <a href="http://www.w3schools.com/JS/js_cookies.asp" target="_blank">W3 Schools</a> for a detailed description of these JavaScript functions.  The only edit I have made is with the <b>getCookie</b> function in lines 13-14 where it retrieves the cookie value and inserts the content into a div.  Also, line 18 is very important as it calls the <b>getCookie</b> function and looks for the <b>content</b> cookie.  If the cookie exists, the content will be displayed in the <b>myDiv2</b> div.  </p>
<p>When using these JavaScripts, <b> make sure you load them after all your HTML is loaded</b>.  Otherwise, the function would have no place to insert the cookie content.</p>
<p>Although using cookies is a great way to store information, there are a couple of caveats:</p>
<ol>
<li><b>Size limitation</b> &#8211; This depends on the browser but keeping your cookies to a maximum size of 4000 bytes will keep you in the safe zone.</li>
<li><b>Cookie limit per domain</b> &#8211; This also depends on the browser but the general rule is 20 cookies per domain.</li>
</ol>
<p><script lang="javascript">getCookie('content');</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/04/14/how-to-dynamically-add-content-to-a-div-via-javascript/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>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>Knix</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.
 
function toggle() {
	var ele = document.getElementById("toggleText");
	var text = document.getElementById("displayText");
	if(ele.style.display == "block") {
    		ele.style.display = "none";
		text.innerHTML = [...]]]></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 language="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("name");
                  if (name == 'newboxes') {
                        if (newboxes[x].id == thechosenone) {
                        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 10px;
}
#headerDivImg {
     background-color: #0037DB;
     color: #9EB6FF;
}
#contentDivImg, #contentDivImg_ {
     background-color: #FFE694;
}
#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 ==></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>
<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"><div class="code"><pre class="javascript 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: #003366; 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: #003366; 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: #003366; 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: #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>;
		text.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;show&quot;</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>;
		text.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;hide&quot;</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></div></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>
<ul>
<li><b>If the display style is <code>none</code></b>, the function will:</li>
<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>
<li><b>If the display style is <code>block</code>, the function will:</b></li>
<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>
</ul>
<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"><div class="code"><pre class="javascript 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: #003366; 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: #003366; 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: #003366; 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: #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>;
		text.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;show&quot;</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>;
		text.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;hide&quot;</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></div></div>

<style type="text/css">
#headerDiv, #contentDiv {
     float: left;
     width: 510px;
}
#titleText {
     float: left;
     font-size: 1.2em;
     font-weight: bold;
     margin: 5px 10px;
}
#headerDiv {
     background-color: #0037DB;
     color: #9EB6FF;
}
#contentDiv {
     background-color: #FFE694;
}
#myContent {
     margin: 5px 10px;
}
#headerDiv a {
     float: right;
     margin: 10px 10px 5px 5px;
}
#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">Random Snippets 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.2em;<br />
     font-weight: bold;<br />
     margin: 5px 10px;<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"><div class="code"><pre class="html" style="font-family:monospace;">&lt;div id=&quot;headerDiv&quot;&gt;
     &lt;div id=&quot;titleText&quot;&gt;Random Snippets 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></div></div>

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

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> 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: #003366; 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: #003366; 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: #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>;
		text.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;restore&quot;</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>;
		text.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;collapse&quot;</span>;
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<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;">
            <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;">Div #1</div>
</td>
<td>
<div style="border: 1px solid blue; background-color: #99CCFF; padding: 5px;">
            <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;">Div #2</div
      </td>
<td>
<div style="border: 1px solid blue; background-color: #99CCFF; padding: 5px;">
            <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;">Div #3</div
      </td>
</tr>
</table>
<input type="button" value="Press me to toggle all 3 DIVs" onClick="toggle3(['myContent1', 'myContent2', 'myContent3'], ['myHeader1', 'myHeader2', 'myHeader3']);">
</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 javascript" style="font-family:monospace;"><span style="color: #003366; 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> Array<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>0; i <span style="color: #339933;">&lt;</span> contentDiv.<span style="color: #660066;">length</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: #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: #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;&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;&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: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;&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;&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;&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>
<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>
<div id="box1" style="border: 1px solid black; background-color: #CCCCCC; display: none;padding: 5px;">First</div>
</td>
<td>
<div id="box2" style="border: 1px solid black; background-color: #CCCCCC; display: none;padding: 5px;">Second</div
      </td>
<td>
<div id="box3" style="border: 1px solid black; background-color: #CCCCCC; display: none;padding: 5px;">Third</div
      </td>
</tr>
</table>
<input id="toggleButton" type="button" value="Show me the money!" onclick="counter++; toggle4('box');">
</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&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&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&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 javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> counter <span style="color: #339933;">=</span> 0;
<span style="color: #003366; font-weight: bold;">var</span> numBoxes <span style="color: #339933;">=</span> <span style="color: #CC0000;">3</span>;
<span style="color: #003366; 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: #003366; 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: #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: #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: #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: #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>
<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 javascript" style="font-family:monospace;"><span style="color: #003366; 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: #003366; 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: #003366; 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: #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>;
		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: #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>;
                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: #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>
<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 name="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 name="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 name="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 name=&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 name=&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
      &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 name=&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
      &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 javascript" style="font-family:monospace;"><span style="color: #003366; 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: #003366; 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: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> x<span style="color: #339933;">=</span>0; x<span style="color: #339933;">&lt;</span>newboxes.<span style="color: #660066;">length</span>; x<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                  <span style="color: #000066;">name</span> <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;name&quot;</span><span style="color: #009900;">&#41;</span>;
                  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span> <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: #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: #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 =)</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>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/02/12/how-to-hide-and-show-your-div/feed/</wfw:commentRss>
		<slash:comments>157</slash:comments>
		</item>
	</channel>
</rss>
