append elements to HTML DOM

  • 2 Replies
  • 1433 Views
*

Karen

  • *
  • 17
append elements to HTML DOM
« on: May 08, 2016, 19:04:21 pm »
What is the most efficient way ?

*

Ruhit

  • *
  • 22
Re: append elements to HTML DOM
« Reply #1 on: May 19, 2016, 16:23:02 pm »
You could do it easily with jQuery:

Code
$("#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:

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

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

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

*

Karen

  • *
  • 17
Re: append elements to HTML DOM
« Reply #2 on: May 19, 2016, 17:20:14 pm »
Thanks again ruhit!

 

Sitemap 1 2 3 4 5 6 7 8