How to get the second class name from element?

Solution 1:

You can use split like this:

alert($('#divID').attr('class').split(' ')[1]);

To get all classes, you can do this instead:

var classes = $('#divID').attr('class').split(' ');

for(var i=0; i<classes.length; i++){
  alert(classes[i]);
}

More Info:

  • http://www.w3schools.com/jsref/jsref_split.asp

Solution 2:

// alerts "8"
alert($('<div class="something 8"></div>').attr('class').split(' ')[1]);