Is there a way to empty a div leaving only elements with a specific class name? Or, is there a way to remove all elements within a div leaving only elements with a specified class?
+8
A:
This should do the trick:
$('#theDiv').find('*').not('.className').remove();
Sample markup:
<div id="theDiv">
<p>this will be removed</p>
<p class="className">this will stay</p>
</div>
Marve
2009-08-11 21:12:32
excellent answer Marve, thank you
Ronal
2009-08-11 21:31:19
you should accept his answer if it works.
Jason
2009-08-11 21:43:08
@Jason - I'm all for Marve's answer - but what if they both work? He should accept the answer that is most useful to him.
karim79
2009-08-11 21:57:00