- Home
- /
- Tutorials
- /
- JavaScript Tutorial
- /
- JavaScript Inserting Elements
JavaScript DOM
JavaScript Inserting Elements
append,prepend,beforeandafterdecide exactly where a new element lands.
Creating an element is only half the job; the other half is placing it.
These four methods cover every position you are likely to want.
Example
Example
javascript
document.body.innerHTML = '<ul id="list"><li>Two</li></ul>';
const li = document.createElement("li");
li.textContent = "One";
document.getElementById("list").prepend(li);
console.log(document.querySelector("li").textContent);