How to Add new elements dynamically in javascript ?

Bharathi Cherukuri
Posted by Bharathi Cherukuri under JavaScript category on | Points: 40 | Views : 1367
Here is an example to add new elements dynamically in javascript.

Example:


<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>t1</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>

Comments or Responses

Login to post response