junreyd
junreyd

Reputation: 47

Remove multiple specific childNodes in JavaScript

I just wonder on how to remove specific html elements inside div.

Using JavaScript, I research already removing child but I found only removed one element with the array order like [0] and removed all child. My problem is how to remove the specific html multiple tags element.

Need to remove all <Ul> elements inside #custom_description_video div

Here is the sample image:

enter image description here

Upvotes: 0

Views: 809

Answers (2)

charlietfl
charlietfl

Reputation: 171700

You can use Element.remove() in a loop

const uls = document.querySelectorAll('#custom_description_video > ul')

uls.forEach(el => el.remove())
<div id="custom_description_video">
  <div>Some div</div>
  <div>Another div</div>
  <ul>
    <li>One</li>
    <li>Two</li>
    <li>Three</li>
  </ul>
  <ul>
    <li>One</li>
    <li>Two</li>
    <li>Three</li>
  </ul>

</div>

Upvotes: 2

Riyaz Khan
Riyaz Khan

Reputation: 3268

Here is another approach to remove all child tag elements:

var parentElement = document.getElementById('custom_description_video');


Array.prototype.slice.call(parentElement.getElementsByTagName('ul')).forEach(function(item) { item.parentNode.removeChild(item); } );

Upvotes: 1

Related Questions