jQuery - find last class on the element

jquery, jquery-selectors

Solution

The simplest solution is to use `pop`, which accesses the last element in the array.

var lastClass = $('div').attr('class').split(' ').pop();

Note that `pop` also removes the element from the array, but since you aren't doing anything else with it, that's not a problem.

Problem

How to find last class on the element without knowing exact number of classes? Our element: ``` <div class="class-1 class-2 some-other-class"></div> ``` Normal approach with split will not work here, as we do not know the number of classes. Can we check the length of the `.split(' ')` ? ``` var i = $('div').prop('class'); var j = i.split(' ')[x]; ``` Any suggestions much appreciated.

Original source