Here is an alternative that doesn't use a counter.
http://jsfiddle.net/mqchen/9LHE5/
var classes = ["red", "blue", "green", "papayawhip"];
$("#something").mouseenter(function(e) {
classes.push(classes.shift());
$(this).removeClass(classes[classes.length - 1]).addClass(classes[0]);
});
Basically, it removes the first element from the array, using shift
, appends it to the end of the array, then removes that class (which is now at the end of the array) from the element, and then adds the first element from the array (which used to be the second element), using push
.
EDIT: Here is a revision that starts on the first class in the array instead of the second. http://jsfiddle.net/mqchen/9LHE5/2/