Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery delete all children except a div

Tags:

jquery

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>
like image 457
coure2011 Avatar asked Feb 22 '12 06:02

coure2011


People also ask

How to remove all child in jQuery?

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.

How to remove elements with jQuery?

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.

How to remove html element by id in jQuery?

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.


1 Answers

var save = $('#test .notsure').detach();
$('#test').empty().append(save);
like image 73
nullaware Avatar answered Oct 16 '22 04:10

nullaware