Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get specific elements that have no CSS class using jQuery [duplicate]

Let's say I have a page with multiple h2 tags. I want to get all the h2 tags that do not have a 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 to get the h2 elements wrapping "Headline 1" and "Headline 3" in the above example.

Doing this:

var h2_tags = $("h2");

Will result in getting all H2's, which I don't want. How can I get only the ones without any CSS class?

like image 385
Ricketts Avatar asked Dec 04 '22 11:12

Ricketts


1 Answers

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

$('h2:not([class])')
like image 177
Elliot Bonneville Avatar answered Feb 13 '23 03:02

Elliot Bonneville