Q:

Jquery plugin : how to call internal function from outside

(function ($) {
    $.fn.extend({
        leanModal : function (options) {
            var defaults = {
                top : 100,
                overlay : 0.5,
                closeButton : null
            };
            var overlay = $("<div id='lean_overlay'></div>");
            $("body").append(overlay);
            options = $.extend(defaults, options);
            return this.each(function () {
                var o = options;
                $(this).click(function (e) {
                    var modal_id = $(this).attr("href");
                    //$("#lean_overlay").click(function () {
                    //  close_modal(modal_id)
                    //});
                    $(o.closeButton).click(function () {
                        close_modal(modal_id)
                    });
                    var modal_height = $(modal_id).outerHeight();
                    var modal_width = $(modal_id).outerWidth();
                    $("#lean_overlay").css({
                        "display" : "block",
                        opacity : 0
                    });
                    $("#lean_overlay").fadeTo(200, o.overlay);
                    $(modal_id).css({
                        "display" : "block",
                        "position" : "fixed",
                        "opacity" : 0,
                        "z-index" : 11000,
                        "left" : 50 + "%",
                        "margin-left" :  - (modal_width / 2) + "px",
                        "top" : o.top + "px"
                    });
                    $(modal_id).fadeTo(200, 1);
                    e.preventDefault()
                })
            });
            function close_modal(modal_id) {
                $("#lean_overlay").fadeOut(200);
                $(modal_id).css({
                    "display" : "none"
                })
            }
        }
    })
})(jQuery);

this is from leanModal plugin - http://leanmodal.finelysliced.com.au/ 
how should i call this close_modal() function outside of above plugin? i want to close the popup dialog from an ajax call's success callback. ajax call is inside an external js function.

A:

So long as you don't mind altering the source provided by the vendor (as given in your initial question) the following changes should work (please note this hasn't been tested fully, but it should get you most of the way there):
(function ($) {
$.fn.extend({
    leanModal: function (method) {
        var methods = {
            init: function (options) {
                return this.each(function () {
                    var o = options;
                    $(this).click(function (e) {
                        var modal_id = $(this).attr("href");
                        //$("#lean_overlay").click(function () {
                        //  close_modal(modal_id)
                        //});
                        $(o.closeButton).click(function () {
                            close_modal(modal_id)
                        });
                        var modal_height = $(modal_id).outerHeight();
                        var modal_width = $(modal_id).outerWidth();
                        $("#lean_overlay").css({
                            "display": "block",
                            opacity: 0
                        });
                        $("#lean_overlay").fadeTo(200, o.overlay);
                        $(modal_id).css({
                            "display": "block",
                            "position": "fixed",
                            "opacity": 0,
                            "z-index": 11000,
                            "left": 50 + "%",
                            "margin-left": -(modal_width / 2) + "px",
                            "top": o.top + "px"
                        });
                        $(modal_id).fadeTo(200, 1);
                        e.preventDefault()
                    })
                });
            },
            close: function (modal_id) {
                close_modal(modal_id);
            }
        };

        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.leanModal');
        }  

        var defaults = {
            top: 100,
            overlay: 0.5,
            closeButton: null
        };            
        var overlay = $("<div id='lean_overlay'></div>");
        $("body").append(overlay);
        options = $.extend(defaults, options);

        function close_modal(modal_id) {
            $("#lean_overlay").fadeOut(200);
                $(modal_id).css({
                    "display": "none"
                })
            }
        }
    })
})(jQuery);

Then, you will be able to call your code like this:
     $('#foo').leanModal(); //Initialise

...

     $('#foo').leanModal('close'); //Close

A:

The point of this module construct is to avoid publishing some variables (including functions).
So you can't access what isn't referenced in the returned object.
Read this about this construct.
In your case, if you don't want to change the plugin to move the function in the returned object, the simplest is probably to do 
$("#lean_overlay").fadeOut(200);
$(modal_id).css({
    "display" : "none"
})

where modal_id is the href attribute of your element.

