How to remove all child nodes from a parent node using jQuery?


Use the jQuery empty() method to remove all child nodes from a parent node.

Example

You can try to run the following code to remove all child nodes from a parent node using jQuery:

Live Demo

<!DOCTYPE html>
<html>
<head>
<style>

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

});
</script>
</head>

<body>
  <div>
    <ul>
      <li>li (child)</li>
      <li>li (child)</li>
      <li>li (child)</li>
      <li>li (child)</li>
    </ul>  
  </div>
  <button id="button1">Remove</button>
  <p>Click Remove to remove all child nodes.</p>
</body>
</html>

Updated on: 15-Jun-2020

287 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements