<?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; getElementById</title>
	<atom:link href="http://www.randomsnippets.com/tag/getelementbyid/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 dynamically remove/delete elements via JavaScript</title>
		<link>http://www.randomsnippets.com/2008/03/26/how-to-dynamically-removedelete-elements-via-javascript/</link>
		<comments>http://www.randomsnippets.com/2008/03/26/how-to-dynamically-removedelete-elements-via-javascript/#comments</comments>
		<pubDate>Thu, 27 Mar 2008 05:57:53 +0000</pubDate>
		<dc:creator>Knix</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[
function removeElement(parentDiv, childDiv){
     if (childDiv == parentDiv) {
          alert("The parent div cannot be removed.");
     }
     else if (document.getElementById(childDiv)) {     
          var [...]]]></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>
<input type="button" value="Remove Element" onClick="removeElement('parent','child');">
</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:<br />
<input id="nameOfChild" type="text" value="child2">
<input type="button" value="Remove Element" onClick="var name=document.getElementById('nameOfChild').value; removeElement('parentDiv', name);">
<p>For those who are lazy in typing in the actual names, we have these handy-dandy buttons:</p>
<input type="button" value="Remove child1" onClick="removeElement('parentDiv', 'child1');">
<input type="button" value="Remove child2" onClick="removeElement('parentDiv', 'child2');">
<input type="button" value="Remove child3" onClick="removeElement('parentDiv', 'child3');">
<input type="button" value="Remove parentDiv" onClick="removeElement('parentDiv', 'parentDiv');">
</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>
<input id="cb1" type="checkbox" value="childOne" onclick="removeElement('parentDivElement', this.value);"><label for="cb1">Remove <b>childOne</b></label></p>
<input id="cb2" type="checkbox" value="childTwo" onclick="removeElement('parentDivElement', this.value);"><label for="cb2">Remove <b>childTwo</b></label></p>
<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>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/03/26/how-to-dynamically-removedelete-elements-via-javascript/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
	</channel>
</rss>
