The jQuery empty() method removes only the child elements of the selected element.
Syntax:
$(selector).empty()
Example1:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("#div1").empty(); }); }); </script> </head> <body> <div id="div1" style="height:100px;width:200px;border:1px solid black;background-color:cyan;"> Hello! </div> <br> <button>Empty the div element</button> </body> </html>
Example2:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("#div1").empty(); }); }); </script> </head> <body> <div id="div1" style="height:100px;width:200px;border:1px solid black;background-color:cyan;"> <div id="div2" style="height:50px;width:20px;border:1px solid black;background-color:yellow;"> </div> </div> <br> <button>Empty the div element</button> </body> </html>