by Sai gowtham

How to remove the Html element with JavaScript

In this tutorial, we will learn how to remove the html elements from a dom with the help of JavaScript.

Removing Html elements

We need to use the element.remove() method to remove the html element from a dom tree.

Example:

Consider we have the following Html.

<div>
   <h1 id="tt-1">Title 1</h1>   <h2 id="tt-2">Title 2</h2>
</div>

Now, we are removing the h1 element from a dom tree by calling the element.remove() method.

const h1 = document.getElementById('tt-1');

h1.remove();

Similarly, we can also use the removeChild() method.

const h1 = document.getElementById('tt-1');

h1.parentNode.removeChild(h1);

Top Udemy Courses

JavaScript - The Complete Guide 2020 (Beginner + Advanced)
JavaScript - The Complete Guide 2020 (Beginner + Advanced)
26,545 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
221,520 students enrolled
44 hours of video content
View Course
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
114,575 students enrolled
21 hours of video content
View Course