Home » Javascript

Javascript: Add Remove HTML elements

17 November 2009 1,549 views Popularity: 3% Share/Bookmark

You can add html elements dynamically with Javascript. In this article, I will show you how to do so. I have created two Javascript functions. One for creating html elements and the other for removing them.

In my html elements, I have created ‘li’ and ‘strong’ elements inside ‘div’. The div id is ‘summary’. I have written comment in the code so that it would be easy to understand.

Javascript function to create html elements:-

function createSummary(title,content) {
	// get div id
	var summaryOptions = document.getElementById('summary');

	// create new <li>
	var newLi = document.createElement('li');
	var liIdName = title;
	// set <li> attribute id
	newLi.setAttribute('id',liIdName);

	// create new element <strong>
	var newStrong = document.createElement('strong');
	var strongIdName = title+" strong";
	// assign id to <strong>
	newStrong.setAttribute('id',strongIdName);

	// adding parameter 'title' value inside <strong>
	newStrong.innerHTML = title;
	// adding <strong> inside div
	summaryOptions.appendChild(newStrong);

	// adding parameter 'content' value inside <li>
	newLi.innerHTML = content;
	// adding <li> inside div
	summaryOptions.appendChild(newLi);
}

Javascript function to remove html elements:-

function removeSummary(title) {
	var summaryOptions = document.getElementById('summary');
	var childLi = document.getElementById(title);
	var childStrong = document.getElementById(title+" strong");

	// remove <li>
	if(childLi) {
	summaryOptions.removeChild(childLi);
	}

	// remove strong <strong>
	if(childStrong) {
	summaryOptions.removeChild(childStrong);
	}
}

Full code is as under:-


<html>
<head>
<title>Test</title>
<script language="javascript" type="text/javascript">
function createSummary(title,content) {
	// get div id
	var summaryOptions = document.getElementById('summary');

	// create new <li>
	var newLi = document.createElement('li');
	var liIdName = title;
	// set <li> attribute id
	newLi.setAttribute('id',liIdName);

	// create new element <strong>
	var newStrong = document.createElement('strong');
	var strongIdName = title+" strong";
	// assign id to <strong>
	newStrong.setAttribute('id',strongIdName);

	// adding parameter 'title' value inside <strong>
	newStrong.innerHTML = title;
	// adding <strong> inside div
	summaryOptions.appendChild(newStrong);

	// adding parameter 'content' value inside <li>
	newLi.innerHTML = content;
	// adding <li> inside div
	summaryOptions.appendChild(newLi);
}

function removeSummary(title) {
	var summaryOptions = document.getElementById('summary');
	var childLi = document.getElementById(title);
	var childStrong = document.getElementById(title+" strong");

	// remove <li>
	if(childLi) {
		summaryOptions.removeChild(childLi);
	}

	// remove <strong>
	if(childStrong) {
		summaryOptions.removeChild(childStrong);
	}
}
</script>
</head>

<body>

<input type="button" value="Show" onclick="createSummary('This is title','This is content.')" />
<input type="button" value="Remove" onclick="removeSummary('This is title')" />
<div id="summary" style="border:1px solid #CCCCCC; width:400px; ">&amp;nbsp;</div>

</body>
</html>

From Mukesh Chapagain's Blog, post Javascript: Add Remove HTML elements

Related posts:

  1. Javascript: Show/Hide HTML elements
  2. print_r in Javascript
  3. Javascript: How to Submit form and Change form action?
  4. Website statistic (User Information) in Javascript
  5. Go back link in Javascript
  6. PHP Javascript : Playing with multi-dimensional array
  7. Javascript: How to get current URL without Query string?
  8. How to get(view) html source code of a website
  9. Blogspot: How to hide or remove Blogger top navigation bar?
  10. Javascript: Show Hide textbox text on focus