Reputation: 1033
I've got a big HTML page. Some elements (can be p, h1, div etc.) are tagged with the class 'keep_me'. I need to remove all the elements present in the page WITHOUT this class? Any idea on how to do it with jQuery?
I tried with something like that, but it doesn't work (obviously ;) :
jQuery('#content *').remove(":not('[class=keep_me]')");
Upvotes: 8
Views: 11054
Reputation: 239312
Use not():
The .not() method is typically faster and may end up providing you with more readable selections than pushing complex selectors or variables into a :not() selector filter.
$('#content *').not('.keep_me').remove();
Upvotes: 5
Reputation: 816580
Just do:
jQuery('#content :not(.keep_me)').remove();
See the documentation:
jQuery(':not(selector)')
Selects all elements that do not match the given selector.
Upvotes: 20