views:

44

answers:

2

The element looks like this:

<li class="blah active"> ... </li>

jQuery.attr('class') will return both classes. How can I get only the 1st class ('blah' in this case) with jQuery?

A: 
var first = jQuery.attr('class').split(" ")[0];
galambalazs
Note that `jQuery.attr('class')` is wrong, there should be a selector too.
Sarfraz
You're soo smart... I've just preserved the form of the question. If he doesn't know how to use jQuery than it's useless for him to get the first class of an element lol :)
galambalazs
and he said "`jQuery.attr('class')` will return both classes", which indicates that by `jQuery` he meant an instance of the jQuery object, not the contructor function which invokes `jQuery.fn.init`
galambalazs
+1  A: 

You need to split that into an array:

alert(jQuery('selector').attr('class').split(' ')[0]);
Sarfraz
Or use a regex: `var first = $('#foo').attr('class').replace(/^(\S*).*/, '$1');`
Pointy
@Pointy: Yup, thanks for adding that.
Sarfraz
cool, works. thanks :P
Alex