EN
JavaScript - remove existing HTML element
0 points
In this article, we would like to show you how to remove existing HTML element using remove()
method in JavaScript.
Quick solution:
xxxxxxxxxx
1
// find element by id
2
var element = document.querySelector("#element-id");
3
4
//remove element
5
element.remove();
Note:
Some browsers may not support the
remove()
method. Go to this article to see alternative solution.
In this example, we will remove child-2
element.
xxxxxxxxxx
1
2
<html>
3
<body>
4
<div>
5
<p id="child-1">This is a paragraph 1.</p>
6
<p id="child-2">This is a paragraph 2.</p>
7
</div>
8
<script>
9
var element = document.querySelector("#child-2");
10
element.remove();
11
</script>
12
</body>
13
</html>