Fake Fish Fake Fish - 19 days ago 5
Javascript Question

Is it possible to remove dom node from detached dom tree using jquery?

What I'm trying to do:

$domTree = $('<div class="container"><div class="first">first</div><div class="second">second</div></div>')
$domTree.remove('.first')
$dom.html() // Should contain second div only


What I get: both first and second divs are present.

What I'm doing wrong? What's the right approach?

Answer

Yes you can , use find to get the element and remove to remove it, end is used to return back the container

$domTree = $('<div class="container"><div class="first">first</div><div class="second">second</div></div>').find('.first').remove().end();
$domTree.html();

$domTree = $('<div class="container"><div class="first">first</div><div class="second">second</div></div>').find('.first').remove().end();
    console.log($domTree[0]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>