jQuery get last class name from element


A select tag that has 2 class names, how can I get the last class name using the $(this) of that select tag?

something like this maybe…

  var x = $(this).attr('class').lastIndexOf(' ');
  var y = $(this).attr('class').substr(x);

let me just say… this is an “OK, but could cause problems” way to do it… if an extra space is left after the 2nd class… you’re going to get back nothing…

If anyone’s got a better solution… post it… :slight_smile:

EDIT: you can probably also use RegEx… but where’s the risk in that? :smiley:

Here you go:

$(this).attr('class').split(' ').slice(-1); 

Okay, thanks guys, I’ll try this out.

or something like this:

$(this).attr('class').replace(/[-\s\w]*?([-\w]+)\s?$/, '$1');

haha, so many options!