Creating and Inserting Elements
Build new nodes with createElement and append.
Building New Elements
Instead of writing HTML strings, you can construct elements in code. This avoids XSS and gives you a real element reference to configure.
createElement
document.createElement makes a new, detached element. It exists in memory but is not yet in the page.
const li = document.createElement("li");
li.textContent = "New item";All lessons in this course
- Changing Text and HTML Content
- Creating and Inserting Elements
- Modifying Attributes and Classes
- Removing and Replacing Nodes