JavaScript's Creating Elements

Using JavaScript, there are two ways to create HTML dynamically.  The first is using strings:

myDiv.innerHTML = "<span>Some HTML content</span>";

THe other approach is using the Document Object Model (DOM), which has a series of methods to create HTML elements via:

var span = document.createElement("span");
span.innerHTML = "Some HTML Content";
myDiv.appendChild(span);

The createElement method creates HTML elements, which appendChild appends them to the DOM tree.  So a SPAN element is appended to a DIV.  An attribute can be set via the setAttribute method, or retrieved via the getAttribute method as well.

Comments

No Comments