I saw a few posts here about 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 a button that, when pressed, can cycle through classes 3 . But if in this case there is a need to cycle through 4, 5 or more classes, you can easily scale the script.
From now on, I can βquoteβ between the two classes, which basically βswitchesβ more than the bike, so for this I have:
HTML:
<a href="#" class="toggle">Toggle classes</a> <div class="class1">...</div>
JQuery
$('.toggle').click(function () { $('div').toggleClass('class1 class2'); });
Here is a simple fiddle of this.
Now you would (well, I) think that adding a third class to the method would work, but it is not:
$('div').toggleClass('class1 class2 class3');
It happens that switching starts only between class1 and class3 .
So here is where I have my initial problem: how do I get the Toggle button to switch through classes 3 sequentially?
And then: What if someone needs to switch to 4, 5, or more classes?
source share