2

Is there a build in way to "Rotate" a list of classes in JQuery similar to toggle class?

For example:

$("#MyElt").rotateClass("Cl1 Cl2 Cl3");
$("#MyElt").rotateClass("Cl1 Cl2 Cl3");
$("#MyElt").rotateClass("Cl1 Cl2 Cl3");
$("#MyElt").rotateClass("Cl1 Cl2 Cl3");

The status of the class on MYElt after each call would be:

Cl1
Cl2
Cl3
NO CLASS
3
  • Are you looking for horizontal rotate or vertical rotate? Commented Feb 18, 2012 at 13:49
  • @SivaCharan Not a visual rotation, more of a cycling through a list of classes. Commented Feb 18, 2012 at 13:54
  • possible duplicate of jQuery: Rotating elements through a set of classes Commented Feb 18, 2012 at 14:06

2 Answers 2

2

For your quick reference, I have pasted the code from jQuery: Rotating elements through a set of classes.

I think, this should help you.

$.fn.rotateClass(/* pass multiple class names here */) {
    for (var i = 0; i < arguments.length; i++) {
        if (this.hasClass(arguments[i])) {
            this.removeClass(arguments[i]));
            i++;
            if (i >= arguments.length) {
                i = 0;
            }
            this.addClass(arguments[i]);
            return(this);
        }
    }
    // none found so set the first class
    if (arguments.length > 0) {
        this.addClass(arguments[0]);
    }
    return(this);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! That will teach me to use Google search rather than the SO search.
Shouldn't it be $.fn.rotateClass = function() { ...?
Check also my version of the plugin here: stackoverflow.com/a/14637729/1329367
0

Using hasClass() and toggleClass, you can make your own.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.