How do I delete everything inside a div
except a div/span
with class='notsure'
? I can delete all children using empty
but not sure how to save a specific div
.
<div id="test">
here is a test
<p>a paragraph</p>
<div class="notsure"></div>
</div>
jQuery empty() Method The empty() method removes all child nodes and content from the selected elements. Note: This method does not remove the element itself, or its attributes. Tip: To remove the elements without removing data and events, use the detach() method.
Use . remove() when you want to remove the element itself, as well as everything inside it. In addition to the elements themselves, all bound events and jQuery data associated with the elements are removed.
For removing the element using jQuery, we can use element name, element id, element class etc. We can remove a single or multiple elements using jQuery remove() function.
var save = $('#test .notsure').detach();
$('#test').empty().append(save);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With