Kevin G
Kevin G

Reputation: 2455

Pass var between functions on event click

document.getElementById('button').addEventListener('click', function() {
ApiCall()
console.log(lol)

});



function ApiCall() {
var lol = "this is a test";
}

I have looked into the javascript scopes but how would i pass this variable to the first function ?

Upvotes: 0

Views: 35

Answers (1)

Mark
Mark

Reputation: 92440

You should return the value from your function. Like this:

document.getElementById('button').addEventListener('click', function() {
  var passed_lol = ApiCall()
  console.log(passed_lol)
});

function ApiCall() {
  var lol = "this is a test"
  return lol;
}
<input id="button" type="button" onclick=stop() value="clickme">

Upvotes: 2

Related Questions