How to remove all div content but not div using jquery?

How to remove all div content but not div using jquery?

div contains a lot of UL, LI, DIV, 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 delete the entire contents of the container.

thank

+3
source share
2 answers
$("#container").empty();

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

emptytakes precedence over setting an html element to nothing. From the doc:

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

+10
source
$('#container').html('');
+1
source

Source: https://habr.com/ru/post/1761419/


All Articles