<?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; remove</title>
	<atom:link href="http://www.randomsnippets.com/tag/remove/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 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>
