- Home
- /
- Tutorials
- /
- JavaScript Tutorial
- /
- JavaScript Creating Elements
JavaScript DOM
JavaScript Creating Elements
createElementbuilds a new element that can then be added to the page.
A created element exists in memory but is invisible until you attach it.
This is the safe alternative to building pages out of innerHTML strings.
Example
Example
javascript
document.body.innerHTML = '<div id="box"></div>';
const p = document.createElement("p");
p.textContent = "Made by JavaScript";
document.getElementById("box").append(p);
console.log(document.querySelectorAll("p").length);