I am working on a page that will show prices in a choice of currencies (euros, pounds or dollars). My aim is to display the price in euros first, keeping the pound and dollar prices hidden.
<button id="mybutton">Change Currency</button>
<p id="euro" class="shown">Euro</p>
<p id="pound" class="hidden">Pound</p>
<p id="dollar" class="hidden">Dollar</p>
When the button is clicked, I need the three ids to cycle through the three states shown/hidden/hidden, hidden/shown/hidden and hidden/hidden/shown. So far I have made it work with two ids (not difficult!).
$('#mybutton').click(function()
{
$('#euro').toggleClass('hidden','shown'),
$('#pound').toggleClass('hidden','shown');
});
I'm at a loss to see how to extend this to the third id. Any ideas gratefully received.
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:
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.
http://jsfiddle.net/NWj5w/
http://api.jquery.com/toggleClass/
Another working example:
HTML:
JS:
Fiddle
Supposing you don't use those class elsewhere, you could do
Supposing you use those classes elsewhere, I'd recommend you to change your HTML to add a specific classs and to do
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.
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:
The CSS is:
The javascript is:
Doubling up the lines seems a bit messy but it works and my efforts at something more streamlined didn't. So .....
HTML
JavaScript
Fiddle
http://jsfiddle.net/g6EM5/