Given an HTML document containing div elements and the task is to remove the existing HTML elements using jQuery. To remove elements and its content, jQuery provides two methods:
- remove(): It removes the selected element with its child elements.
- empty(): It removes the child element from the selected elements.
Example:
<div id="Parent"> <div id="child1">child 1</div> <div id="child2">child 2</div> <div id="child3">child 3</div> </div>
Syntax: The following function clears the parent “div”
$('#parent').empty();
The following function clears all the child “divs”, but leaves the parent intact.
$('#parent div').empty()
Example:
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content= "width=device-width, initial-scale=1.0"> <script src= </script> </head> <body> <div id="parent"> Parent div <div id="child1">Child 1</div> <div id="child2">Child 2</div> <div id="child3">Child 3</div> </div> <button onclick=myFunc()>Click me</button> <Script> function myFunc() { $('#parent div').empty() } </Script> </body> </html> |
chevron_right
filter_none
Output:
- Before clicking the button:

- After clicking the button:

Recommended Posts:
- How to copy the content of a div into another div using jQuery ?
- How to clear the content of a div using JavaScript?
- How to overlay one div over another div using CSS
- How to center a div within another div?
- How to make an image center-aligned (vertically & horizontally) inside a bigger div using CSS?
- How to scroll to an element inside a div using javascript?
- How to place a div inside an iframe for IE ?
- How to align content of a div to the bottom using CSS ?
- How to make div height expand with its content using CSS ?
- Print the content of a div element using JavaScript
- How to dynamically get the content width of a div using AngularJS ?
- How to detect and change the content/style of a div using jQuery ?
- How to use a div as content for Twitter's Popover?
- How to set div width to fit content using CSS ?
- How to dynamically get the content height of a div using AngularJS ?
- How to make div width expand with its content using CSS ?
- How to put a responsive clear button inside HTML input text field ?
- Projecting Content into Components with ng-content
- How to clear all cookies using JavaScript ?
- How to clear all options in a dropdown box?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


