Search code examples
javascripthtmljqueryremovechildnodelist

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


Solution

  • 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>