4

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?

Ricketts
  • 5,025
  • 4
  • 35
  • 48
  • That's an HTML class, not a CSS class. CSS has class selectors, it doesn't have classes. – Quentin May 06 '13 at 18:22
  • 2
    http://stackoverflow.com/questions/1962247/jquery-get-all-divs-which-do-not-have-class-attribute – Adil Shaikh May 06 '13 at 18:25
  • Thanks everyone for the quick responses. @Quentin - My bad on the terminology, however, I believe everyone will clearly understand what I was referring to. – Ricketts May 06 '13 at 18:34

2 Answers2

11

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

$('h2:not([class])')
Elliot Bonneville
  • 51,872
  • 23
  • 96
  • 123
5
$('h2:not([class])')

should work

vittore
  • 17,449
  • 6
  • 44
  • 82