views:

36

answers:

3

Hello, I'm trying to find a simple way to attach click events to an element, which is quite easy for the 1st and 2nd click events.. But, what I am trying to accomplish is a three click Toggle.

1st click = addClas('one'); 2nd click = removeClass('one').addClass('two'); 3rd click = removeClass('two').addClass('three'); next click would == 1st click and through the loop again..

I'm using a simple div and applying styling through CSS, to change the elements background position - all pretty typical and easy.. and is cake to do for a toggle() or click() event.. but I can't figure out for the life of me how to handle the 3rd click ! :D

here is my markup, for example purposes:

Any help is greatly appreciated !!

UPDATE: I've got something that works.. but, IMHO it's ugly and messy.. there's got to be a cleaner, more concise way of doing this: $(".tri_switch").click(function(){ var this_id = $(this).attr("id");

if( $(this).hasClass("one") ){                  
    $("input#user_"+this_id).attr('checked','checked');
    $("input.tri_switch_radio").not("#user_"+this_id).removeAttr('checked');
    $(this).removeClass('one').addClass('two');
    $("span.tri_switch_checked").text('User');
}
else if ( $(this).hasClass("two") ){
    $("input#admin_"+this_id).attr('checked','checked');
    $("input.tri_switch_radio").not("#admin_"+this_id).removeAttr('checked');
    $(this).removeClass('two').addClass('three');
    $("span.tri_switch_checked").text('Admin');

}
else if ( $(this).hasClass("three") ){
    $("input#readonly_"+this_id).attr('checked','checked');
    $("input.tri_switch_radio").not("#readonly_"+this_id).removeAttr('checked');
    $(this).removeClass('three').addClass('one');
    $("span.tri_switch_checked").text('Readonly');

}

// alert(this_id); });

+2  A: 

Use toggle http://api.jquery.com/toggle/

Codler
Thanks for the link ;) I've used Toggle many times, but from what I know so far, it is only good for first and second triggers.. hence the name. Can you elaborate on your recommendation and how you would use it in this scenario?
revive
@The Artful Benny have wrote an example of toggle for your scenario.
Codler
+3  A: 

Yup @Codler, toggle() can take more than the default two parameters. Just throw in the third parameter:

$('#element').toggle(function(){
  $(this).addClass('one');
},
function(){
  $(this).removeClass('one').addClass('two');
},
function(){
  $(this).removeClass('two').addClass('three');
});
The Artful Benny
B E A U T I F U L ! I thought I had seen a case where someone used toggle() with a third parameter,.. but after searching and finding nothing on it, I chalked it up to bar recollection LOL Thanks, this will work perfectly.
revive
A: 
var counter = 0;
$(".button").bind("click",function(){
    counter++;
    switch(counter){
        case 1:
            doSomething();
            break;
        case 2:
            doSomething2();
            break;
        case3:
            doSomething(3);
            counter=0;
            break;
    }
})

is it what you looking for?

Anakin
in the last case.. did you mean 'case 3' or 'case3' as you typed?Also, in that same case.. you have doSomething(3);should that be doSomething3(); ??Just want to make sure I clearly understand your example.. that I'm not missing something. Thanks
revive
its my typo, sorry.case 3 and doSomething3(). in every case, you can call some functions. just you need to reset the counter your last case. (in this example 3)
Anakin