How to remove all child nodes from a parent in jQuery?


To remove all child nodes from a parent in jQuery, use the empty() method.

Example

The jQuery empty() method removes all child nodes of the set of matched elements from the DOM.

Live Demo

<!DOCTYPE html>
<html>
<head>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("button").click(function() {
    $("#demo").empty();
  });
});
</script>
</head>
<body>

<ul id='demo'>
  <li>India</li>
  <li>US</li>
  <li>UK</li>
</ul>
<button>Remove all child nodes</button>

</body>
</html>

Updated on: 17-Dec-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements