delete all elements with selected id in javascript

You can delete all elements with a selected ID in JavaScript with the use of the removeChild() method.

First, you need to retrieve the parent node of the element with the selected ID using the getElementById() method. Once you have the parent node, you can use the removeChild() method to remove all child elements with the selected ID.

Here's an example code snippet that demonstrates how to accomplish this:

index.tsx
const parent = document.getElementById('parent'); // Retrieve parent node
const child = document.getElementById('selected-id'); // Retrieve child node with selected ID
while (child) {
  parent.removeChild(child); // Remove child from parent node
  child = document.getElementById('selected-id'); // Retrieve next child node with selected ID
}
343 chars
7 lines

In this example, we use a while loop to repeatedly remove all child elements with the selected ID from the parent node until there are no more children with that ID.

gistlibby LogSnag