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

jQueryWeb DevelopmentFront End Technology

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>
raja
Published on 17-Dec-2017 12:08:37
Advertisements