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

1
  • 4
    None of your five other questions has received an acceptable answer? Nov 22, 2010 at 9:37

4 Answers 4

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.

2
  • @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.
    – jensgram
    Nov 22, 2010 at 9: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, 2010 at 9: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.

5
  • @jensgram: Wow, I saw and fixed that almost instantly, talk about timing. :-) Nov 22, 2010 at 9:41
  • @T.J. Crowder And I took the liberty to change my gender :)
    – jensgram
    Nov 22, 2010 at 9: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). Nov 22, 2010 at 9:44
  • @T.J. Crowder :) In this case it's CONCAT(<firstName>, <lastName>), though.
    – jensgram
    Nov 22, 2010 at 9:47
  • 1
    @jens Hehe, it's not actually the first time @T.J. has got gender wrong! Hilarious comments :)
    – alex
    May 10, 2011 at 10:25
2
$(this).attr('class').split(' ').pop()
0
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"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.