Syntax
document.createElement(tag)
parent.appendChild(child)
element.textContent = textExamples
Creating and Appending an Element
Building a new element from scratch and adding it to the page.
const newItem = document.createElement("li");
newItem.textContent = "New list item";
newItem.classList.add("list-item");
const list = document.querySelector("#myList");
list.appendChild(newItem);
// append() is a newer alternative that also accepts plain strings
list.append("Some plain text", newItem);textContent vs innerHTML
The important security and behavior difference between the two.
const div = document.querySelector("#output");
const userInput = "<img src=x onerror='alert(1)'>";
div.textContent = userInput; // SAFE - displayed as literal text, not executed
div.innerHTML = userInput; // DANGEROUS if userInput is untrusted - can execute scriptsRemoving Elements
Cleanly removing elements from the DOM.
const item = document.querySelector("#tempMessage");
item.remove(); // modern, simple way to remove an element
// Older approach, still seen in some codebases:
// item.parentNode.removeChild(item);Best practices
- Use textContent instead of innerHTML whenever you are inserting plain text, especially anything derived from user input, to avoid XSS vulnerabilities
- Only use innerHTML with content you fully trust and control, or after properly sanitizing it
- Use element.remove() for removing an element - it is simpler than the older parentNode.removeChild() pattern
- Batch multiple DOM insertions where possible (e.g., building a DocumentFragment) instead of appending one element at a time, for better performance on large updates
At a glance
- Purpose
- Application logic and interaction
- File extension
- .js ยท .mjs
- Runs in
- Browsers and JavaScript runtimes
- Usually used with
- HTML, CSS and Web APIs
Specifications & further reading
Related JavaScript documentation
DOM Selection: querySelector(), getElementById()
These methods find elements in the DOM. getElementById() finds a single element by its unique id, generally the fastest option. querySelector() finds the first element matching any valid CSS selector, and querySelectorAll() returns all matching elements as a static NodeList.classList: add, remove, toggle, contains
The classList property provides a convenient API for managing an element's CSS classes without manually parsing the className string. add() and remove() add or remove one or more classes, toggle() switches a class on or off based on its current presence, and contains() checks whether a class is currently applied.IntersectionObserver
IntersectionObserver efficiently detects when an element enters or exits the viewport (or another container), without the performance cost of manually listening to scroll events and calculating positions. It is the standard tool for lazy-loading images, infinite scroll, and scroll-triggered animations.MutationObserver
MutationObserver watches for changes to the DOM tree - added/removed elements, attribute changes, or text content changes - and runs a callback in response. It is useful for reacting to DOM changes made by other scripts or third-party widgets that you do not directly control.
These methods find elements in the DOM. getElementById() finds a single element by its unique id, generally the fastest option. querySelector() finds the first element matching any valid CSS selector, and querySelectorAll() returns all matching elements as a static NodeList.classList: add, remove, toggle, contains
The classList property provides a convenient API for managing an element's CSS classes without manually parsing the className string. add() and remove() add or remove one or more classes, toggle() switches a class on or off based on its current presence, and contains() checks whether a class is currently applied.IntersectionObserver
IntersectionObserver efficiently detects when an element enters or exits the viewport (or another container), without the performance cost of manually listening to scroll events and calculating positions. It is the standard tool for lazy-loading images, infinite scroll, and scroll-triggered animations.MutationObserver
MutationObserver watches for changes to the DOM tree - added/removed elements, attribute changes, or text content changes - and runs a callback in response. It is useful for reacting to DOM changes made by other scripts or third-party widgets that you do not directly control.