Bart
Bart

Reputation: 4920

How I Remove all Div contents but not the div using 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

Upvotes: 3

Views: 642

Answers (2)

karim79
karim79

Reputation: 342795

$("#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.

Upvotes: 10

treeface
treeface

Reputation: 13351

$('#container').html('');

Upvotes: 1

Related Questions