How to delete items without a specified class

I have a large HTML page. Some elements (may be p, h1, div, etc.) are marked with the class "keep_me". Do I need to delete all elements that are present on the page WITHOUT this class? Any idea on how to do this using jQuery?

I tried something like this, but it does not work (obviously;):

jQuery('#content *').remove(":not('[class=keep_me]')"); 
+6
jquery jquery-selectors
source share
2 answers

Just do:

 jQuery('#content :not(.keep_me)').remove(); 

See the documentation:

jQuery (': not (selector)')

Selects all elements that do not match the given selector.

+14
source share

Use not () :

The .not () method is usually faster and may ultimately provide you with more readable choices than pressing complex selectors or variables in a selector filter: not ().

 $('#content *').not('.keep_me').remove(); 
+5
source share

All Articles