Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery click / toggle between two functions

I am looking for a way to have two separate operations / functions / "blocks of code" run when something is clicked and then a totally different block when the same thing is clicked again. I put this together. I was wondering if there was a more efficient / elegant way. I know about jQuery .toggle() but it kind of sucks.

Working here: http://jsfiddle.net/reggi/FcvaD/1/

var count = 0; $("#time").click(function() {     count++;     //even odd click detect      var isEven = function(someNumber) {         return (someNumber % 2 === 0) ? true : false;     };     // on odd clicks do this     if (isEven(count) === false) {         $(this).animate({             width: "260px"         }, 1500);     }     // on even clicks do this     else if (isEven(count) === true) {         $(this).animate({             width: "30px"         }, 1500);     } }); 
like image 910
ThomasReggi Avatar asked Feb 06 '11 04:02

ThomasReggi


People also ask

What is toggle () in jQuery?

jQuery toggle() Method The toggle() method toggles between hide() and show() for the selected elements. This method checks the selected elements for visibility. show() is run if an element is hidden.

How do you toggle a button in JavaScript?

We can toggle a button using conditional statements like if-else statement in JavaScript. We can toggle almost all the properties of an element like its value, class, id, and color in JavaScript. To change any property of an element, we need to get the element using its id or class.

How do you add toggle in HTML?

We can do that by using the HTML label tag and HTML input type = checkbox. HTML code: The HTML code is used to create a structure of toggle switch. Since it does not contain CSS so it is just a simple structure.


1 Answers

jQuery has two methods called .toggle(). The other one [docs] does exactly what you want for click events.

Note: It seems that at least since jQuery 1.7, this version of .toggle is deprecated, probably for exactly that reason, namely that two versions exist. Using .toggle to change the visibility of elements is just a more common usage. The method was removed in jQuery 1.9.

Below is an example of how one could implement the same functionality as a plugin (but probably exposes the same problems as the built-in version (see the last paragraph in the documentation)).


(function($) {     $.fn.clickToggle = function(func1, func2) {         var funcs = [func1, func2];         this.data('toggleclicked', 0);         this.click(function() {             var data = $(this).data();             var tc = data.toggleclicked;             $.proxy(funcs[tc], this)();             data.toggleclicked = (tc + 1) % 2;         });         return this;     }; }(jQuery)); 

DEMO

(Disclaimer: I don't say this is the best implementation! I bet it can be improved in terms of performance)

And then call it with:

$('#test').clickToggle(function() {        $(this).animate({         width: "260px"     }, 1500); }, function() {     $(this).animate({         width: "30px"     }, 1500); }); 

Update 2:

In the meantime, I created a proper plugin for this. It accepts an arbitrary number of functions and can be used for any event. It can be found on GitHub.

like image 94
Felix Kling Avatar answered Sep 23 '22 06:09

Felix Kling