Good day. Guys, such a question. There is a script

$(document).ready(function(){ $('.looni').AnyFunc(); }); 

I can not figure out how to make it so that when I clicked it would switch to another code. Those.

 $(document).ready(function(){ if (клик по ссылке) $('.looni').AnyFunc(); else $('.looni').AnyFunc2(); }); 

At the same time it is necessary that when you click again changed in the reverse order. Grubogovrya type switch. With every click to change functions.

    3 answers 3

    Enter both functions in toggle() :

     $('#knopka').toggle( function(e){ // делай раз! }, function(e){ // делай два! } ); 

    A working example .

    • Here, I will add my word, there are obvious disadvantages. You need to initialize a variable 2 times if you want it not to go beyond the scope of toggle. - lampa
    • In the question, not a word about variables. We will not guess what else might be needed) - Sergiks
    • $ ('. looni'). AnyFunc () adds a standard value to the input field. but it is not necessary. even if I make $ ('. looni'). val (); then the value is inserted with the focus on input. cutting the default from the plugin itself is not an option. but $ ('. looni'). AnyFunc ('none') kills standard output. I thought it was easier to make a switch. The toggle looked, he immediately thought about it, but did not know about this solution. I'll keep it on mind. thank. lampa, and what did you mean by initialization 2 times? - drop_off
    • @dropoff jsfiddle.net/PSvPd/1 - lampa

    There are over 9000 options, here is the most simple and visual:

     <html> <head> <script> click = 1 /// объявляем переменную для хранения значения toogle function ToogleClick() { if(click) { alert('1'); /// код в случаи 1 click = 0; } else { alert('0'); /// код в случаи 2 click = 1; } } </script> </had> <body> <div onclick="ToogleClick();" >click me</div> <!-- вызываем функию --> </body> </html> 
       $(document).ready(function(){ var click_check = true; $('a').click(function() { if(click_check) { alert(1); } else { alert(2); } click_check = !click_check; }); }); 
      • @Alexander Sergeev is so correct, yes. But still, the option would work on alert (2) - lampa