0

I have the following code:

function myFunction(){
  var bob = function() {
alert("works");
    } 
  };
//document.getElementById("btn").addEventListener('click', bob); //calls the function 
}

How do I call the bob function outside the myFunction() ? It works with the btn element, but I want it to be called outside

Thanks !

klaus ruprecht
  • 127
  • 1
  • 2
  • 10
  • The click event is happening inside the scope of myFunction which still has access to bob, so it works. However.. you cannot access the function bob outside of the scope my function. Instead try: var bob = function() { alert('works')} function myFunction(){ bob() }; } Now you can also use bob wherever you like. – Vontei Aug 24 '15 at 22:41
  • I would answer with a code block but its blocked now – Vontei Aug 24 '15 at 22:44

1 Answers1

-1

You cannot call bob function from outside myFunction because it doesn't exist in that scope..

Joaquín O
  • 1,431
  • 1
  • 9
  • 16