Iladarsda
Iladarsda

Reputation: 10696

jQuery - if two classes - get the first one

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.

Upvotes: 4

Views: 6169

Answers (4)

Town
Town

Reputation: 14906

Use JavaScript's split function:

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

Demo

Upvotes: 13

a&#39;r
a&#39;r

Reputation: 36999

$('<div class="a b"/>').attr('class').split(' ')[0]

Upvotes: 0

Chris Pratt
Chris Pratt

Reputation: 239290

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

Upvotes: 6

Daniel A. White
Daniel A. White

Reputation: 190945

You can split the string based on the space.

Upvotes: 2

Related Questions