Home > DeveloperSection > Interviews > How to Add new elements dynamically?

Posted on    March-18-2011 12:00 AM

 JavaScript JavaScript 
Ratings:
 1 Answer(s)
  1709  View(s)
Rate this:

Amit Singh

Total Post:565

Points:2825
Posted on    March-18-2011 12:00 AM

Example:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Title of page</title>
<script type="text/javascript">
function addNode() {
var newP = document.createElement("p");
var textNode = document.createTextNode(" I'm a new text node");
newP.appendChild(textNode);
document.getElementById("firstP").appendChild(newP);
}
</script>
</head>
<body onload="addNode();" style=" background: url('../images/Sand-1280.jpg'); background-color: yellow;">
<p id="firstP">firstP<p>
</body>
</html>

Don't want to miss updates? Please click the below button!

Follow MindStick