1

i want get the last item of an array with class names. My code looks like this

var getClassName = [];
getClassName = $(this).attr('class').split();
console.log(getClassName);

In the console i become get this answer

["classname1 classname2"]

how can i get the last class name?

Thanks

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
Peter
  • 683
  • 2
  • 9
  • 13

4 Answers4

4
console.log(getClassName[getClassName.length-1]);

Will do, but you need to pass an argument to split():

var getClassName = $(this).attr('class').split(' ');
var lastIndex = getClassName.length - 1;
console.log(getClassName[lastIndex]);

EDIT: On using this.className
Consider using this.className instead of $(this).attr('class'). This is mentioned in other answers.

Andy E has done a great write-up on how we tend to overuse jQuery: Utilizing the awesome power of jQuery to access properties of an element. The article specifically treats the use of .attr("id") but the issue is the same for $(...).attr('className') vs. this.className.

You could even use

var getClassName = (this.className || '').split(' ');

if you're not sure that .className exists.

Community
  • 1
  • 1
jensgram
  • 31,109
  • 6
  • 81
  • 98
  • @Peter You should really follow @T.J. Crowder's advice and use `this.className` instead of the `.attr()`. I've just made this point in [another thread](http://stackoverflow.com/questions/4243554/jquery-if-checkbox-is-checked-do-this/4243575#4243575). – jensgram Nov 22 '10 at 09:43
  • @Stefan Mai Sort of. `var getClassName = "classname1 classname2".split(); alert(getClassName);` showed that `split()` would *not* use a whitespace as default split char. After editing it was obvious that I was not the only one to find out :) – jensgram Nov 22 '10 at 09:45
2

As jensgram points out, you're nearly there; see his answer for the detail if you want to stay jQuery-specific.

But you're making the browser do a lot of extra work, this is one of those times you really don't need jQuery:

var getClassName;
getClassName = this.className.split(' ');
console.log(getClassName[getClassName.length-1]);

The className property of DOM elements is supported by all major browsers (and probably all minor ones).

Still, unless you're doing this in a tight loop, the extra overhead of the $() and attr calls probably doesn't really matter much.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • @jensgram: Wow, I saw and fixed that almost instantly, talk about timing. :-) – T.J. Crowder Nov 22 '10 at 09:41
  • @T.J. Crowder And I took the liberty to change my gender :) – jensgram Nov 22 '10 at 09:42
  • 5
    @jensgram: Thanks. You're going to laugh, but I read "jensgram" as "Jen's gram" as in "Jen's grandmother" and didn't apply my probability filter. :-) And I'm sure we have a couple of grandmothers here on SO, but I'm thinking not that many (probably more grandfathers). – T.J. Crowder Nov 22 '10 at 09:44
  • @T.J. Crowder :) In this case it's `CONCAT(, )`, though. – jensgram Nov 22 '10 at 09:47
  • 1
    @jens Hehe, it's not actually the first time @T.J. has got gender wrong! Hilarious comments :) – alex May 10 '11 at 10:25
2
$(this).attr('class').split(' ').pop()
alex
  • 479,566
  • 201
  • 878
  • 984
Mikael Eliasson
  • 5,157
  • 23
  • 27
1

Note the documentation for split:

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

You need to write:

split(' ')

Without the separator "the entire string will be returned"

Stefan Mai
  • 23,367
  • 6
  • 55
  • 61