Three-way Toggling Of Two States (javascript Or Jquery)
Solution 1:
I just wanted to point out you may be confused on how toggleClass works. The second parameter is never a string like a class. Instead, it's a boolean. I've gotten rid of the "shown" class (things are shown by default) and used a boolean for the second argument:
i=0;
$('#mybutton').click(function(){
i++;
$('#euro').toggleClass('hidden', i%3!==0),
$('#pound').toggleClass('hidden',i%3!==1);
$('#dollar').toggleClass('hidden',i%3!==2);
});
All this does is remove the hidden class when the cycling matches (i%3===0) and add it (hide those elements) otherwise.
If you did want to toggle between multiple classes, I believe the first argument should be a space separated list of classes.
Solution 2:
Supposing you don't use those class elsewhere, you could do
var i = 0, divs = $('.hidden, .shown');
$('#mybutton').click(function() {
$('.shown').removeClass('shown').addclass('hidden');
i = (i+1) % divs.length;
divs.eq(i).removeClass('hidden').addclass('shown');
});
Supposing you use those classes elsewhere, I'd recommend you to change your HTML to add a specific classs and to do
var i = 0, divs = $('.specificClass');
$('#mybutton').click(function() {
$('.shown').removeClass('shown').addclass('hidden');
i = (i+1) % divs.length;
divs.eq(i).removeClass('hidden').addclass('shown');
});
Note that I find usually simpler to change only one class, that is to give all the elements the same "specific" class and to just add or remove the "hidden" class.
Solution 3:
My final solution is based on Jere's answer above, but is a little more complicated. As well as showing a price in one currency and hiding the other's, I also need to indicate which currency is being displayed and which others are available. Therefore my html ends up like this:
<spanclass="euro_h activ">Euro</span><spanclass="pound_h activ inactiv">Pound</span><spanclass="dollar_h activ inactiv">Dollar</span><spanid="change"style="cursor:pointer">Change Currency</span><br><br><spanclass="euro shown">€ 100</span><spanclass="pound hidden">£ 80</span><spanclass="dollar hidden">$ 140</span>
The CSS is:
.activ {color:#ff0000}
.inactiv {color:#CCCCCC}
.shown {display:inline}
.hidden {display:none}
The javascript is:
i=0;
$('#change').click(function(){
i++;
$('.euro').toggleClass('hidden', i%3!==0);
$('.pound').toggleClass('hidden',i%3!==1);
$('.dollar').toggleClass('hidden',i%3!==2);
$('.euro_h').toggleClass('inactiv', i%3!==0);
$('.pound_h').toggleClass('inactiv',i%3!==1);
$('.dollar_h').toggleClass('inactiv',i%3!==2);
});
Doubling up the lines seems a bit messy but it works and my efforts at something more streamlined didn't. So .....
Solution 4:
HTML
<button id="mybutton">Change Currency</button>
<p id="euro" class="unit shown">Euro</p>
<p id="pound" class="unit hidden">Pound</p>
<p id="dollar" class="unit hidden">Dollar</p>
JavaScript
$("#mybutton").on("click", function() {
var btns = $(".unit");
var active = btns.filter(".shown").removeClass("shown").next();
active = (active.length) ? active : btns.eq(0)
active.addClass("shown");
});
Fiddle
Solution 5:
Another working example:
HTML:
<buttonid="mybutton">Change Currency</button><pclass="currency euro">Euro</p><pclass="currency pound">Pound</p><pclass="currency dollar">Dollar</p>
JS:
$('#mybutton').click(function() {
for (var i = 0; i < currencies.length; i++) {
i == shown ? currencies[i].show() : currencies[i].hide();
}
shown = (shown+1)%3;
});
Post a Comment for "Three-way Toggling Of Two States (javascript Or Jquery)"