Get specific elements that don't have a CSS class using jQuery

Let's say I have a page with several h2 tags. I want to get all h2 tags that do not have css class.

So in this example:

<h2>Headline 1</h2> <p>content 1</p> <h2 class="some-class">Headline 2</h2> <p>content 2</p> <h2>Headline 3</h2> <p>content 3</p> <h2 class="another-class">Headline 4</h2> <p>content 4</p> 

I want the h2 elements to wrap "Heading 1" and "Heading 3" in the above example.

Performing this action:

 var h2_tags = $("h2"); 

The result is all of H2, which I don’t want. How can I get only those who don't have a CSS class?

+4
source share
2 answers

[class] is a valid selector, so you can simply do this:

 $('h2:not([class])') 
+11
source
 $('h2:not([class])') 

must work

+5
source

Source: https://habr.com/ru/post/1479407/


All Articles