How to remove particular div element in JavaScript
In this article, we will talk about how to remove a particular div element by using JavaScript.
There are many ways to remove a specific element. Among them, we will share three methods.
- Using the removeChild() method.
- Using outerHTML property.
- By using the remove() method.
[1] remove div element using removeChild() method. #
The removeChild() method removes a specific child element from its parent element.
function removeDiv(){
let mainSection = document.querySelector('#main-sec');
let myDiv = document.querySelector('#my-div');
mainSection.removeChild(myDiv);
}
[2] remove particular element using outerHTML property. #
To remove an element using the outerHTML property, we have to set its content to "" (blank).
function removeDiv(){
let myDiv = document.querySelector('#my-div');
myDiv.outerHTML = "";
}
[3] remove specific element using remove() method. #
<div id="my-div">Div element.</div>
<button onclick="removeDiv()">Remove Div</button>
<script>
function removeDiv(){
let myDiv = document.querySelector('#my-div');
myDiv.remove()
}
</script>
save
listen
AI Answer
How to remove particular div element in JavaScript
0
In this article, we will talk about how to remove a particular div element by using JavaS…
asked
Apu
0 answers
2915
In this article, we will talk about how to remove a particular div element by using JavaS…
Answer Link
answered
Apu
