Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I Remove all Div contents but not the div using jquery?

Tags:

jquery

How I Remove all Div contents but not the div using jquery?

the div contains many ULs, LIs, DIVs, H1s

<div id="container">
  <ul>
      <li>test</li>
      <li>test</li>
  </ul>
  <div><h1>test</h1></div>
  <div><h1>test</h1></div>
  <div><h1>test</h1></div>
</div>

I would like to remove all the contents of container.

Thanks

like image 310
Bart Avatar asked Nov 20 '25 17:11

Bart


1 Answers

$("#container").empty();

http://api.jquery.com/empty/

empty has an advantage over setting the element's html to nothing. From the documentation:

To avoid memory leaks, jQuery removes other constructs such as data and event handlers from the child elements before removing the elements themselves.

like image 69
karim79 Avatar answered Nov 24 '25 23:11

karim79



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!