0

I know this is a duplicate topic, but this topic is different because i use very simple examples. I have a JavaScript function like this:

(function myfunction($){
    function a(){
        alert("A section"); 
    }
    function b(){
        alert("B section")
    }
})();

I want to create a HTML button which calls function A, and function B. How can i do that?

1
  • 3
    You cannot access those functions from outside the closure. Commented Mar 13, 2013 at 15:10

4 Answers 4

2

Make them global by declaring the names outside the closure, then assign them within the closure.

var a, b;
(function myfunction($){
    a = function() {
        alert("A section"); 
    }
    b = function() {
        alert("B section")
    }
})();

You can reduce the pollution of the global namespace by wrapping them in an object:

var myfuns = (function myfunction($) {
                 return { a: function() {alert("A section");},
                          b: function() {alert("B section");}
                        };
              })();

Then call myfuns.a and myfuns.b.

Sign up to request clarification or add additional context in comments.

4 Comments

It would be nice to other posters if you accepted answers in your old questions, too.
I'll do it. btw, I new to this site.
Huh? You've been here for a year and a half!
Yes, but i didn't understood the potential of stackoverflow. I didn't took stackoverflow seriously, until now.
1

You can create an object that has these function and call your functions like below :

myFunctions = {

  a : function(){ alert("A section"); },
  b : function(){ alert("B section"); }

}

and then call them like below :

myFunctions.a();
myFunctions.b();

This is the jsfiddle to check it.

UPDATE:

As answer to your comment, this is an updated jsfiddle to show you how it works from HTML.

2 Comments

This method can't call myFunctions.a(); and myFunctions.b(); through a HTML button.
Yes you can, if you are including your javascript in the header.
0

Assign function to global variables in closure is one way. Other way to do it is following.

(function myfunction($){
    $("#button1").click(function {
        alert("A section"); 
    });
    $("#button2").click(function {
        alert("B section"); 
    });
})(jQuery);

This doesn't spoil your global scope. And binds on click event for your buttons.

When not using jQuery

(function myfunction($){
    var button1 = getElementById("idOfButton1");
    button1.onclick = function {
        alert("A section"); 
    };
    var button2 = getElementById("idOfButton2");
    button1.onclick = function {
        alert("B section"); 
    };
})();

1 Comment

When did he say he's using jQuery?
0

You can create a variable first, and then expose it to global context:

(function myfunction($){
    var global = {
        a: function(){
            alert("A section");
        },
        b: function(){
            alert("B section")
        }
    };

    window.global = global;
})();
global.a();
global.b();

3 Comments

@Mathletics Would you mind telling me if I mislead something?
Well you didn't use any jQuery, so I don't know what that statement is supposed to mean.
@Mathletics Thanks for telling me. I don't mean I use jQuery code here. In jQuery, we can find such code: "window.jQuery = window.$ = jQuery;", so I use the similar syntax in the answer. I've deleted the ambiguous words.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.