<?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>logon2 Blog &#187; Javascript</title>
	<atom:link href="http://www.logon2.com.au/blog/archive/category/coding/javascript-coding/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.logon2.com.au/blog</link>
	<description>Better use of the web for everybody</description>
	<lastBuildDate>Tue, 13 Jul 2010 12:44:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>Adding, Removing and Editing Elements Dynamically using JavaScript</title>
		<link>http://www.logon2.com.au/blog/archive/web-design/adding-removing-and-editing-elements-dynamically-using-javascript/</link>
		<comments>http://www.logon2.com.au/blog/archive/web-design/adding-removing-and-editing-elements-dynamically-using-javascript/#comments</comments>
		<pubDate>Sat, 19 Jan 2008 03:54:34 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[add]]></category>
		<category><![CDATA[element]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.logon2.com.au/blog/archive/web-design/adding-removing-and-editing-elements-dynamically-using-javascript/</guid>
		<description><![CDATA[While working myLookAgain (the second attempt), I&#8217;ve had to use a little bit more AJAX through JavaScript. One of the reasons was that I needed to allow users to add as many &#8216;pieces&#8217; to each look and delete them if they want &#8211; preferably without reloading. In JavaScript terms, I needed to be able to [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.logon2.com.au/blog/wp-content/dynamic-elements.jpg" alt="Using Javascript to Dynamically Add and Remove Elements" align="right" vspace="5" hspace="5" /> While working myLookAgain (the second attempt), I&#8217;ve had to use a little bit more AJAX through JavaScript. One of the reasons was that I needed to allow users to add as many &#8216;pieces&#8217; to each look and delete them if they want &#8211; preferably without reloading.</p>
<p>In JavaScript terms, I needed to be able to add (or create) and remove (or delete) elements, within another element, the form. After a little bit of research on the internet, I had what I needed to do just that. <a href="http://www.logon2.com.au/examples/javascript-add-remove-elements/">See a basic example of javascript dynamically adding and removing elements&#8230;</a> Here&#8217;s how to do it:</p>
<p>To begin with, here&#8217;s the commented JavaScript code for adding an element using the DOM in JavaScript</p>
<pre>
function appendElement(containerId, newElementId, newElementContent) {
	//First, we need to create a new DIV element
	var newElement=document.createElement("div");
	//New we will give it the specified ID so we can manage it later if necessary
	newElement.setAttribute("id", newElementId);
	//Insert the HTML content into the new element
	newElement.innerHTML=newElementContent;

	//Get a reference to the element that will contain the new element
	var container = document.getElementById(containerId);
	//Now we just need to insert our new element into the containing element
	container.appendChild(newElement, container);
}
</pre>
<p>And here&#8217;s the commented JavaScript code for a function to remove elements:</p>
<pre>
	function removeElement(parentId, elementId) {
	//Get a reference to the element containgint the element we are removing
	var parentElement = document.getElementById(parentId);
	//Get a reference to the element we are removing
	var childElement = document.getElementById(elementId);
	//remove the
	parentElement.removeChild(childElement);
}
</pre>
<p>And that&#8217;s it. Of course, this by itself isn&#8217;t going to do much. Though with a little bit of imagination, these functions can help you improve the usability or usefulness of your website.</p>
<h3>Related Posts:</h3><ul class="related_post"><li><a href="http://www.logon2.com.au/blog/archive/troubleshooting/accessible-valid-dropdown-menu-or-popup-menu-using-css-and-semantic-html/" title="Accessible, Valid Dropdown Menu or Popup Menu using CSS and Semantic HTML">Accessible, Valid Dropdown Menu or Popup Menu using CSS and Semantic HTML</a></li><li><a href="http://www.logon2.com.au/blog/archive/coding/coding-php/processing-php-code-in-css-files-to-improve-productivity-and-browser-cross-compatibility-using-apache/" title="Processing PHP Code in CSS files to Improve Productivity and Browser Cross-Compatibility (using Apache)">Processing PHP Code in CSS files to Improve Productivity and Browser Cross-Compatibility (using Apache)</a></li><li><a href="http://www.logon2.com.au/blog/archive/coding/pretty-smarties/pretty-smarties-05-pretty-urls-smarty-templates-and-faster-web-design/" title="Pretty Smarties 0.5 &#8211; Pretty URLs, Smarty Templates and Faster Web Design">Pretty Smarties 0.5 &#8211; Pretty URLs, Smarty Templates and Faster Web Design</a></li><li><a href="http://www.logon2.com.au/blog/archive/web-design/introducing-the-lovely-caption-maker/" title="Introducing the Lovely Caption Maker">Introducing the Lovely Caption Maker</a></li><li><a href="http://www.logon2.com.au/blog/archive/coding/coding-php/extract-get-variables-from-url-string-to-array-php-function/" title="Extract GET Variables from URL String to Array &#8211; PHP Function">Extract GET Variables from URL String to Array &#8211; PHP Function</a></li><li><a href="http://www.logon2.com.au/blog/archive/tools/myspace-convert-text-to-html/" title="MySpace &#8211; Convert Text to HTML">MySpace &#8211; Convert Text to HTML</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.logon2.com.au/blog/archive/web-design/adding-removing-and-editing-elements-dynamically-using-javascript/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

