Cameron
Cameron

Reputation: 28843

jQuery click event like a toggle

Take a look at the following code:

$(document).ready(function()
            {
                $('#link').click(function()
                {
                    alert('1');
                },
                function()
                {
                    alert('2');
                });
            });

Alert 2 will never happen! However I'm wanting to make a link do something different on it's next click so like it's an off and on switch. How do I do this in jQuery?

Upvotes: 1

Views: 660

Answers (3)

keeganwatkins
keeganwatkins

Reputation: 3686

$().toggle() does this.

pass as many functions as you like, and it will execute them in order.

$("#link").toggle(
        function() {
            alert("1")
        },
        function() {
            alert("2")
        }
    );

hope that helps! cheers.

Upvotes: 1

Andy
Andy

Reputation: 30135

$('#link').toggle(function(){
    alert('1');
},function(){
    alert('2');
});

http://jsfiddle.net/GaL9P/

Upvotes: 4

DrStrangeLove
DrStrangeLove

Reputation: 11567

$(document).ready(function()

$('#link').toggle(function() {
  alert('1');
}, function() {
  alert('2');
});

});

Upvotes: 1

Related Questions