tags:

views:

47

answers:

2

Not sure if you can do this, but I want to select the first of two classes of an element with jQuery and return it's first class only.

<div class="module blue">

I want to return 'module'.

tried this:

var state = $('body').attr('class').first();

but none of that seems to work, thanks for any advice.

+3  A: 

Try

var class = $('.module').attr('class');
var st = class.split(' ');
var firstClass = st[0];
Rigobert Song
rocking! thanks guys, a bit of sleep and advice and it all makes sense :-)
gleddy
+3  A: 

Once you have a reference to the element, simply get it's className attribute and split it by space, and then you'll have the first class at [0] in the split array:

var className = $(element).attr('class'),
    split = className.split(/\s+/g);

alert(split[0] || 'Empty className');
J-P
+1 I like this one
c0mrade