Ask questions and get good answers on jQuery

I need to verify if a class has been added to a certain element without knowing which... How can I get the last class setted on a DOM element?

asked Oct 22 '09 at 18:10

Matteo's gravatar image

Matteo
112

edited Oct 22 '09 at 22:02

Matteo%20Bicocchi's gravatar image

Matteo Bicocchi ♦♦
1776129


In jQuery $(element).attr("class") returns the list of all classes of the element as string space-delimited; you can set an array by splitting this string by " " and get last:

var lastElClass = $(element).attr("class").split("").slice(-1);

answered Oct 22 '09 at 18:12

Matteo%20Bicocchi's gravatar image

Matteo Bicocchi ♦♦
1776129

Your answer
toggle preview

jQuery main site - Matteo Bicocchi