MikeSwanson
MikeSwanson

Reputation: 497

How can I remove everything inside of a <div>

I have the following:

<div id="test">

...
...

</div>

I would like to remove all of the elements within the div so I tried:

$('#test > div').remove();

But this doesn't seem to work. Am I doing the right thing here?

Upvotes: 6

Views: 22254

Answers (4)

Yuda Prawira
Yuda Prawira

Reputation: 12471

should this is works too

$('#test > div ').text('');

Upvotes: 1

xkeshav
xkeshav

Reputation: 54032

try with right syntax

Remove : Remove the set of matched elements from the DOM.

 $('div#test').remove();

try with empty

empty : Remove all child nodes of the set of matched elements from the DOM.

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

see html() also, sometime it is helpful

html: When .html() is used to set an element's content, any content that was in that element is completely replaced by the new content.

Note: To remove the elements without removing data and events, use .detach() instead.

Upvotes: 17

kobe
kobe

Reputation: 15835

jsFiddle here

http://jsfiddle.net/53ZRw/

this should do it.

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

if you want to completly remove you can use

.remove();

Upvotes: 2

Marcel
Marcel

Reputation: 28087

$("#test").empty() should do the trick.

Upvotes: 3

Related Questions