Home » Javascript

Javascript: Add Remove HTML elements

17 November 2009 376 views No Comment Popularity: 4% 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. Website statistic (User Information) in Javascript
  4. Go back link in Javascript
  5. PHP Javascript : Playing with multi-dimensional array

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.