Last updated: Mar 5, 2024
Reading time·3 min
Use the replaceChildren()
method to clear the content of a div
element,
e.g. div.replaceChildren()
.
When the replaceChildren
method is invoked without passing it any arguments,
the method empties the element of all its children.
Here is the HTML for the examples.
<!DOCTYPE html> <html lang="en"> <head> <title>bobbyhadz.com</title> <meta charset="UTF-8" /> </head> <body> <div id="container"> <p>Apple</p> <p>Pear</p> <p>Banana</p> </div> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const container = document.getElementById('container'); container.replaceChildren();
We called the
replaceChildren()
method without passing it any parameters, which cleared the contents of the
div
element.
const container = document.getElementById('container'); const p1 = document.createElement('p'); p1.innerHTML = `<span style="background-color: lime">Apple</span>`; const p2 = document.createElement('p'); p2.innerHTML = `<span style="background-color: orange">Orange</span>`; container.replaceChildren(p1, p2); // ✅ If you have a collection of elements // use spread operator (...) to unpack the collection // container.replaceChildren(...[p1, p2]);
By using the replaceChildren()
method, we can clear the contents of the div
and add a new set of children to it in a single step, which is a bit faster.
Node
objects or strings, with which it replaces the element's existing children.Here is an example that clears the contents of a div element on a button click.
<!DOCTYPE html> <html lang="en"> <head> <title>bobbyhadz.com</title> <meta charset="UTF-8" /> </head> <body> <div id="container"> <p>Apple</p> <p>Pear</p> <p>Banana</p> </div> <button id="btn">Clear div contents</button> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const container = document.getElementById('container'); const btn = document.getElementById('btn'); btn.addEventListener('click', function handleClick() { container.replaceChildren(); });
We added a click
event listener to the button element.
Every time the button is clicked, the handleClick
function is invoked, where
we simply call the replaceChildren
method.
Alternatively, you can set the element's textContent
property to an empty
string.
Setting textContent
on the element removes all of its children and replaces
them with a single text node of the provided value.
const container = document.getElementById('container'); container.textContent = '';
The textContent property can be used to read and set the text content of an element.
When using the property to set a node's text content, all of the element's children are removed and replaced with the provided string value.
You might also see the innerHTML
property being used in a similar way.
const container = document.getElementById('container'); container.innerHTML = '';
This achieves the same result, however, it might be slower because setting the
innerHTML
property uses the browser's HTML parser.
The textContent
property doesn't use the browser's HTML parser and might be
faster if the element has many children.
Which approach you pick is a matter of personal preference. I'd go with the
replaceChildren
method as it is direct, easy to read and can be used to
replace the removed child elements in a single step.
You can learn more about the related topics by checking out the following tutorials: