Categories
JavaScript Answers

How to Remove the Parent HTML Element Using JavaScript?

Spread the love

Sometimes, we want to remove the parent HTML element using JavaScript.

In this article, we’ll look at how to remove the parent HTML element using JavaScript.

Remove the Parent HTML Element Using JavaScript

To remove the parent HTML element using JavaScript, we can use parentElement.remove method.

For instance, if we have the following HTML:

<div>  
  <p>  
    hello world  
  </p>  
</div>

Then we can remove the p element’s parent, which is the div by writing:

document.querySelector('p').parentElement.remove();

We just select the p element with:

document.querySelector('p')

Then we call parentElement.remove() on it.

Conclusion

To remove the parent HTML element using JavaScript, we can use parentElement.remove method.

Leave a Reply

Your email address will not be published. Required fields are marked *