Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove everything inside of a <div>

Tags:

jquery

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?

like image 733
MikeSwanson Avatar asked Dec 02 '22 02:12

MikeSwanson


2 Answers

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.

like image 74
diEcho Avatar answered Jan 04 '23 23:01

diEcho


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

like image 27
Marcel Avatar answered Jan 04 '23 23:01

Marcel