1
(function($){
    $.fn.the_func = function() {

        function my_func(){
            alert('it works');
        }

        my_func();

        // other code

    };
})(jQuery);

$(window).load(function(){
    my_func(); // This way?
    $.the_func().my_func(); // Or this way? No?
    $.the_func.my_func(); // No?
    // ?
});

$(document).ready(function(){
    $('div').the_func();
});

How can I call this function outside the function which wraps it?
I want to call my_func() like in this code-example.
(the window-load function is just an example.)
I want to call my_func() from "everywhere" without executing other functions or code within the_func(). BUT I want to use the variables of the_func().
With my_func() I want to update values which are stored in parameters of the_func().

1
  • How about calling it like $("div").the_func("my_func")? Would that work? I don't mean with your current solution...I mean if you would be okay with that syntax, because that's the way I know hot to structure it to make it work as you want Commented Nov 29, 2012 at 14:22

1 Answer 1

2

Here's an example of how I usually write a plugin and could applied to your situation:

http://jsfiddle.net/pMPum/1/

(function ($) {
    function my_func(element) {
        console.log("it works: " + element.innerHTML);
    }

    var methods = {
        init: function (options) {
            console.log("from init");
            console.log("options for init: " + JSON.stringify(options));
            my_func(this);
        },

        my_func: function (options) {
            console.log("from my_func");
            console.log("options for my_func: " + JSON.stringify(options));
            my_func(this);
        }
    };

    $.fn.the_func = function (method) {
        var args = arguments;
        var argss = Array.prototype.slice.call(args, 1);

        return this.each(function () {
            if (methods[method]) {
                methods[method].apply(this, argss);
            }
            else if (typeof method === "object" || !method) {
                methods.init.apply(this, args);
            }
            else {
                $.error("Method " + method + " does not exist on jQuery.the_func");
            }
        });
    };
})(jQuery);

$(document).ready(function () {
    $("div").the_func({    // Same as passing "init" and { } as params
        test: "testing"
    });
});

Notice how I made a generic my_func function inside the scope that can be called. The my_func method in methods is what is exposed to the world through the plugin syntax .the_func() and the my_func function is private and inaccessible directly.

The syntax for calling different methods is the same as most/plenty jQuery plugins.

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

2 Comments

thx. but how do i call "my_func" inside the "init"-function? ..why do you use "return this.each(func…)"?
@JohnDoeSmith Just updated my answer. And I use return this.each so that chaining may occur with the jQuery plugin call. So you could do something like $("div").the_func("method").addClass("something").show(); - it makes it so after the the_func call, any jQuery methods are applied to the original selector

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.