tags:

views:

245

answers:

3

Say I have a div and some content inside it.

<div>
      Content
</div>

With JQuery, how do I empty the div without removing the div, only the content inside?

+6  A: 

You can use the empty function to remove all the child nodes (all its content) of an element:

$('#elementId').empty();

The empty function will also remove all the event handlers and the jQuery internally cached data.

CMS
Thank you CMS!
Keith Donegan
You're welcome Keith!
CMS
+3  A: 

If the div has an id, you can do it like this:

$('#id-of-div').html('');

Or you can do all classes of .class-of-div

$('.class-of-div').html('');

Or just all divs

$('div').html('');

EDIT: But empty() (above) would work better.

davethegr8
Thanks for this other suggestion Dave.
Keith Donegan
I think you mean `$('.class-of-div').html('');`
Horace Loeb
@Horace - Haha, yes. Long day at work.
davethegr8
+1  A: 

Html

<div id='emptythis'>
   Content
</div>

Jquery

$('#emptythis').html('');
halocursed