Create a DOM element
How to create a new element and attach it to the DOM tree.
Use the createElement()
method for creating a DOM element:
var el = document.createElement('div');
Fill the new element with any HTML content:
el.innerHTML = '<p>Hello World!</p>';
Alternatively, use DOM methods for creating content nodes and append them to the new element. This approach requires more code, and is in general slower or equally fast as working with innerHTML:
var textnode = document.createTextNode('Hello World!');
el.appendChild(textnode);
"el" can now be inserted into the DOM tree:
document.body.appendChild(el);