0

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

4
  • Do you need to remove all <ul> elements that are direct children of the #custom_description_video element? Commented Mar 31, 2021 at 17:00
  • Yes, that is correct. Commented Mar 31, 2021 at 17:01
  • Why not upload images of code/errors when asking a question? Commented Mar 31, 2021 at 17:02
  • for (const ul of document.querySelectorAll('#cutom_description_video ul')) ul.remove() Commented Mar 31, 2021 at 17:09

2 Answers 2

2

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>

Sign up to request clarification or add additional context in comments.

1 Comment

Very helpful for me, and I'm pretty sure to the others in the future. Thanks.
1

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); } );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.