JavaScript Code

How to remove a div using JavaScript?

Tutorials using JS

Problem Statement

remove div element from the document using JavaScript


to remove a div element from document using JavaScript, we can use remove() method on the element. get the div element, and call remove() method


remove() method removes the calling element from the document


in the HTML, we have a Click me button and a div. when user clicks on the Click me button, we get the div element by id myDiv and remove it from the document using remove()

<!DOCTYPE html>
<h1>JavaScript Tutorial</h1>
<h2>Remove div from document</h2>
<button id="myBtn">Click me</button>
<div id="myDiv" class="card tile active">About</div>

document.getElementById("myBtn").addEventListener("click", function() {
	//get div element
	const myDiv = document.getElementById("myDiv");
	//remove the div element


Copyright @2022