Reputation: 10696
Example:
Here we have <p class="tab1 current"></p>
<p class="tab1 current"></p>
How can I get only the first class?
var GetFirstClass = $('p').attr('class').filter(':first'); ??
var GetFirstClass = $('p').attr('class').filter(':first');
Any help much appreciated.
Upvotes: 4
Views: 6169
Reputation: 14906
Use JavaScript's split function:
split
$('p').attr('class').split(' ')[0]
Demo
Upvotes: 13
Reputation: 36999
$('<div class="a b"/>').attr('class').split(' ')[0]
Upvotes: 0
Reputation: 239290
Upvotes: 6
Reputation: 190945
You can split the string based on the space.
Upvotes: 2