Learning JavaScript? Download my free JavaScript Handbook 🔥

Given an item in the DOM, use querySelector() to identify it, using any CSS selector you want.

In a loop, you basically check if the firstChild property is defined (the element has at least a child) and you remove it.

The loop ends when all children are removed:

const item = document.querySelector('#itemId')
while (item.firstChild) {

Found a typo or problem? Edit this page