0

Can you please take a look at this demo and let ma know how I can call/toggle two functions from outside of the toggle()

function printConsole() {
  console.log("This is From Function A");
}

function printAlert() {
  alert("This is From Function B");
}

$("#toggler").toggle(
  printConsole(); printAlert();
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<button id="toggler">Toggle Fnctions</button>
Tushar
  • 85,780
  • 21
  • 159
  • 179
Suffii
  • 5,694
  • 15
  • 55
  • 92

2 Answers2

1

Try storing functions in an array , utilizing .click() , Array.prototype.reverse()

function printConsole() {
  console.log("This is From Function A");
}

function printAlert() {
  alert("This is From Function B");
}

var fns = [printConsole, printAlert];

$("#toggler").click(function() {
  fns[0]();
  fns.reverse();
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<button id="toggler">Toggle Fnctions</button>
guest271314
  • 1
  • 15
  • 104
  • 177
0

Depreciated in jquery 1.8

use comma, try this code:

$("#toggler").toggle(
  function() {
  console.log("This is From Function A");
},function() {
  alert("This is From Function B");
}
);

reference: https://api.jquery.com/toggle-event/

Update

Use this Instead:

var clicks = true;
$('a').click(function() {
  if (clicks) {
    alert('Function A');
    clicks = false;
  } else {
    alert('Function B');
    clicks = true;
  }
});

reference: Alternative to jQuery's .toggle() method that supports eventData?

Community
  • 1
  • 1
Shaminder Singh
  • 1,283
  • 2
  • 18
  • 31