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

jQueryWeb DevelopmentFront End Technology

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>
raja
Published on 21-Dec-2017 09:37:01
Advertisements