let say there is a parent id which contains many elements and i want to remove all elements except one.
ex. :
<div id = "parent_id">
<div id = "id_1">
<div id = "id_11"> </div>
<div id = "id_11"> </div>
</div>
<div id = "id_2"> </div>
<div id = "id_n"> </div> // No need to remove this id_n only
</div>
As i can remove innerHTML like this document.getElementId('parent_id').innerHTML = '';
but i need not to remove id_n
. is there any way to do that using javascript or jQuery.
Deleting all children other than id_n with jQuery:
If you'are going to delete the parent_id as well:
I think the Attribute Not Equals selector makes sense here.
Demo.
For fun, POJS is a tad more code, but no jQuery :-)
A lot faster too. ;-)