4

Example:

Here we have <p class="tab1 current"></p>

How can I get only the first class?

var GetFirstClass = $('p').attr('class').filter(':first'); ??

Any help much appreciated.

Iladarsda
  • 10,640
  • 39
  • 106
  • 170

4 Answers4

13

Use JavaScript's split function:

$('p').attr('class').split(' ')[0]

Demo

Town
  • 14,706
  • 3
  • 48
  • 72
6
$('p').attr('class').split(' ')[0]
Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • +1 for getting the same answer as me just before me, but missing out on the accepted answer! – Town Jul 05 '11 at 17:18
2

You can split the string based on the space.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
0
$('<div class="a b"/>').attr('class').split(' ')[0]
a'r
  • 35,921
  • 7
  • 66
  • 67