Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: Toggling between 3 classes (initially)

I've seen several posts here on SO but they are too specific in functionality and structure, and what I'm looking for is something more universal that I or anyone can use anywhere.

All I need is to have a button that when clicked can cycle between 3 classes. But if the case arises to have to cycle through 4, 5 or more classes, that the script can be easily scaled.

As of this moment I am able to 'cycle' between two classes which is basically more "toggling" than cycling, so for that I have:

HTML:

<a href="#" class="toggle">Toggle classes</a>
<div class="class1">...</div>

jQuery:

$('.toggle').click(function () {
  $('div').toggleClass('class1 class2');
});

Here's a simple fiddle of this.

Now, you would (well, I) think that adding a third class to the method would work, but it doesn't:

$('div').toggleClass('class1 class2 class3');

What happens is that the toggling starts happening between class1 and class3 only.

So this is where I have my initial problem: How to have the Toggle button cycle sequentially through 3 classes?

And then: What if someone needs to cycle to 4, 5 or more classes?

like image 789
Ricardo Zea Avatar asked Jan 07 '13 17:01

Ricardo Zea


People also ask

How do you toggle with multiple classes?

Answer. Yes, you can toggle multiple classes using a single . toggleClass() call. To do so, you can separate the class names with spaces.

What does toggleClass do in jQuery?

The toggleClass() method toggles between adding and removing one or more class names from the selected elements. This method checks each element for the specified class names. The class names are added if missing, and removed if already set - This creates a toggle effect.

What is the difference between toggle and toggleClass in jQuery?

The toggle method makes an element visible or hidden, whichever is the opposite of what it is already. It's like using hide or show but you don't need to know which one to pick, it performs like the one that will make a difference. The toggleClass method does something similar to an element's class.

How do I toggle and off in jQuery?

jQuery toggle() MethodThe toggle() method toggles between hide() and show() for the selected elements. This method checks the selected elements for visibility. show() is run if an element is hidden. hide() is run if an element is visible - This creates a toggle effect.


2 Answers

You can do this :

$('.toggle').click(function () {
  var classes = ['class1','class2','class3'];
  $('div').each(function(){
    this.className = classes[($.inArray(this.className, classes)+1)%classes.length];
  });
});

Demonstration

like image 94
Denys Séguret Avatar answered Nov 09 '22 04:11

Denys Séguret


var classes = ['class1', 'class2', 'class3'],
    currentClass = 0;

$('.toggle').click(function () {

    $('div').removeClass(classes[currentClass]);

    if (currentClass + 1 < classes.length)
    {
        currentClass += 1;
    }
    else
    {
        currentClass = 0;
    }

    $('div').addClass(classes[currentClass]);

});

Something like that should work OK :)

Tinker IO link - https://tinker.io/1048b

like image 27
Toddish Avatar answered Nov 09 '22 05:11

Toddish