How to remove specific div element by using JavaScript?
Last Updated :
27 Jun, 2024
Removing a specific `<div>` element with JavaScript is crucial for dynamic web content management. Whether updating information, responding to user actions, or optimizing performance by cleaning the DOM, efficient DOM manipulation is essential. JavaScript offers diverse methods such as `removeChild`, `parentNode.removeChild`, `remove`, and jQuery for removing elements. This guide explores these techniques, empowering developers to choose the best approach based on project requirements.
These are the following methods:
Using parentNode.removeChild() method
This parentNode.removeChild() method removes a specified child node from the DOM tree and returns the removed node.
Syntax:
element.parentNode.removeChild(element);
Example: This example uses the parentNode.removeChild() method to remove a specific 'div' element.
html
<!DOCTYPE html>
<html>
<head>
<title>
Remove specific divisible
element using Javascript
</title>
</head>
<body style="text-align: center;">
<h1 style="color: green;">
GeekforGeeks
</h1>
<div id="gfg_up" style="font-size: 15px;
font-weight: bold;">
A Computer Science Portal for Geeks
</div>
<br />
<button onclick="GFG_click()">
click to remove
</button>
<hr />
<div id="gfg_down" style="font-size: 15px;
font-weight: bold;">
A Computer Science Portal for Geeks
</div>
<script type="text/javascript">
function GFG_click() {
let gfg_down =
document.getElementById("gfg_down");
gfg_down.parentNode.removeChild(gfg_down);
}
</script>
</body>
</html>
Output:

Using outerHTML property
The outerHTML property is used to set the contents of the HTML element. Hence we can remove a specified 'div' element by setting its contents to "" using the outerHTML property.
Syntax:
element.outerHTML=""
Example: This example uses the outerHTML attribute to remove a specific 'div' element.
html
<!DOCTYPE html>
<html>
<head>
<title>
Remove specific divisible
element using Javascript
</title>
</head>
<body style="text-align: center;">
<h1 style="color: green;">
GeekforGeeks
</h1>
<div id="gfg_up" style="font-size: 15px;
font-weight: bold;">
A Computer Science Portal for Geeks
</div>
<br />
<button onclick="GFG_click()">
click to remove
</button>
<hr />
<div id="gfg_down" style="font-size: 15px;
font-weight: bold;">
A Computer Science Portal for Geeks
</div>
<script type="text/javascript">
function GFG_click() {
document.getElementById("gfg_down")
.outerHTML = "";
}
</script>
</body>
</html>
Output:

Using .remove() method
This .remove() method removes the specified div element and all its child nodes.
Syntax:
element.remove();
Example: This example uses the remove() method to remove a specific 'div' element.
html
<!DOCTYPE html>
<html>
<head>
<title>
Remove specific divisible
element using Javascript
</title>
</head>
<body style="text-align: center;">
<h1 style="color: green;">
GeekforGeeks
</h1>
<div id="gfg_up" style="font-size: 15px;
font-weight: bold;">
A Computer Science Portal for Geeks
</div>
<br />
<button onclick="GFG_click()">
click to remove
</button>
<hr />
<div id="gfg_down" style="font-size: 15px;
font-weight: bold;">
A Computer Science Portal for Geeks
</div>
<script type="text/javascript">
function GFG_click() {
let gfg_down =
document.getElementById("gfg_down");
gfg_down.remove();
}
</script>
</body>
</html>
Output:

Using removeChild
Method
This approach involves selecting the parent element and then removing the specific child <div>
.
Example: To demonstrate removing the div using the removeChild method.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Remove Specific Div - removeChild Method</title>
</head>
<body>
<div id="parent">
<div id="div1">Div 1</div>
<div id="div2">Div 2</div>
<div id="div3">Div 3</div>
</div>
<button onclick="removeDiv('div2')">Remove Div 2</button>
<script>
function removeDiv(divId) {
const parent = document.getElementById('parent');
const child = document.getElementById(divId);
if (child) {
parent.removeChild(child);
}
}
</script>
</body>
</html>
Output:
Output
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read