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 2021 (Beginner + Advanced)
JavaScript - The Complete Guide 2021 (Beginner + Advanced)
64,026 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)
284,472 students enrolled
40 hours of video content
View Course
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
152,857 students enrolled
48.5 hours of video content
View Course