TestProject Forum
Powered by leading experts in the test automation community

Append elements to HTML DOM

What is the most efficient way ?

You could do it easily with jQuery:

$("#myDiv").append('<div class="container"><h3>Welcome</h3></div>');

Or you could do it with pure javascript by constructing each element, setting attributes, and then appending:

var newDiv = document.createElement("div");
newDiv.class = "container";

var h3 = document.createElement("h3");
h3.innerHTML = "Welcome";
newDiv.appendChild(h3);

document.getElementById("myDiv").appendChild(newDiv);