In this article, we are going to learn how to manipulate the DOM:
document.createElement(tagName)
creates a new element of tagName.
For example: We want to add another li tag with JavaScript.
const li = document.createElement('li');
This function does NOT put a new element into the DOM. It creates it in memory so we can manipulate the element (such as adding text, styles, classes, ids, etc.) before placing it on the page.
To add text content, we use .textContent
or innerText
to create a text node.
For example, we have these items on the list.
<ul class="items">
<li class="item">Item 1</li>
<li class="item">Item 2</li>
<li class="item">Item 3</li>
</ul>
In this example, we change the text content of the first element to “Good afternoon” and the content of the 2nd element to “Good afternoon”.
const ul = document.querySelector('.items');
ul.firstElementChild.textContent = "Good afternoon";
ul.children[1].innerText = "Good afternoon";
The difference between innerText and textContent is that textContent returns the content of all elements, including <script>
and <style>
elements. In contrast, innerText only shows the visible text contained in a node.
To add HTML content, we use .innerHTML
.
const ul = document.querySelector('.items');
ul.lastElementChild.innerHTML = "<h2>Good evening</h2>";
Using textContent for adding text is recommended because innerHTML can create security risks if misused.
For example, we create an element and insert its text node. However, this element is not part of the DOM tree yet.
To append it to HTML page, we use appendChild() method.
parentNode.appendChild(childNode)
Example:
const li = document.createElement('li');
const innerhtml = li.textContent = "Hello";
const ul = document.querySelector('.items');
ul.appendChild(li);
console.log(ul);
We will get this result.
<ul class="items">
<li class="item">Item 1</li>
<li class="item">Item 2</li>
<li class="item">Item 3</li>
<li>Hello</li>
</ul>
To insert a new element before another element, we use the insertBefore() method:
parentNode.insertBefore(newNode, existingNode)
Example: We insert a new item before the last one in the list.
const ul = document.querySelector(".items");
const li = document.createElement("li");
const innerhtml = (li.textContent = "Hello");
ul.insertBefore(li, ul.lastElementChild);
console.log(ul);
We will get this result.
<ul class="items">
<li class="item">Item 1</li>
<li class="item">Item 2</li>
<li>Hello</li>
<li class="item">Item 3</li>
</ul>
To replace a child element, we use the replaceChild() method.
parentNode.replaceChild(newNode, existingNode)
Example: We replace the 2nd item with a new one.
const ul = document.querySelector(".items");
const li = document.createElement("li");
const innerhtml = (li.textContent = "Hello");
ul.replaceChild(li, ul.children[1]);
console.log(ul);
We will get this result.
<ul class="items">
<li class="item">Item 1</li>
<li>Hello</li>
<li class="item">Item 3</li>
</ul>
We select the ul node.
const ul = document.querySelector('.items');
We can remove all items with the remove() method:
ul.remove(); // All items are gone
Or we can remove the first or last item
ul.firstElementChild.remove(); // Item 1 is gone
ul.lastElementChild.remove(); // Item 3 is gone
We can add an inline style to DOM elements with .style
.
Example:
const divStyle = document.querySelector("div").style;
divStyle.backgroundColor = "blue";
divStyle.border = "2px solid black";
divStyle.width = "80px";
divStyle.height = "50px";
Tips: Check DOM Enlightenment for more style rules.
We can modify class values in an element.
Method | Description | Example |
---|---|---|
className | Set class name | div.className; |
classList.add() | Add one or more class names | div.classList.add(‘btn’); |
classList.toggle() | Toggle a class on or off | div.classList.toggle(‘btn’); |
classList.contains() | Check if class name exists | div.classList.contains(‘btn’); |
classList.replace() | Replace an existing class name with a new one | div.classList.replace(‘old’, ‘new’); |
classList.remove() | Remove a class name | div.classList.remove(‘btn’); |