user3334919 user3334919 -5 years ago 82
jQuery Question

Call a function inside a variable

I'm using this function inside a variable

var MyFunction = (function () {
function FunctionToCall() {

I would like to call
with an event handler from outside the variable like this:


Any idea?

Answer Source

You are probably missing a return statement.
I suppose you intended something like this:

var myFunction = (function() {
  var privateVar = 'Hello';
  return function innerFunction() {
    return privateVar;


Another way is to return an object with the methods that you want to access:

var myThing = (function() {
  var privateVar = 'Hello';

  return {
    innerFunction1: function() {
      return privateVar;
    doSomething: function(newString) {
      privateVar = newString;


After that you can attach anything you made accessible to your event handler.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download