Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript:DIV AppendChild

In the below code "objTo" is a div to which i need to insert multiple number of div. when i use the code for the first time its working.but on the next time its overwriting the existing code.

     <script>

var divtest= document.createElement("div");        
divtest.innerHTML = "<div>new div</div>"         
objTo.appendChild(divtest)
    </script>

Where am i going wrong?

like image 984
user1357872 Avatar asked Apr 26 '12 06:04

user1357872


1 Answers

I have made a very simple working version for you :

http://jsfiddle.net/hQKy9/

Multiple clicks works the whole time :

Script

function addDiv() {
    var objTo = document.getElementById('container');
    var divtest = document.createElement("div");
    divtest.innerHTML = "new div";
    objTo.appendChild(divtest);
}

Html

<div id="container"></div>

<input type="button" onclick="addDiv();" value="Click here to add div"/>
like image 153
Marc Uberstein Avatar answered Sep 19 '22 07:09

Marc Uberstein