... jQuery.attr('class') will return both c...">

JQuery - get first class only from element

The item is as follows:

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

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

+50
javascript jquery class
Jul 08 '10 at 13:17
source share
2 answers

You need to split that into an array:

 console.log(jQuery('selector').attr('class').split(' ')[0]); 
+110
Jul 08 '10 at 13:20
source share
 var first = jQuery.attr('class').split(" ")[0]; 
+5
Jul 08 '10 at 13:19
source share



All Articles