views:

46

answers:

1

I have a jQuery function (stripped down just for this example):

(function($) {

    $.fn.Lightbox = function(options) {

       var opts = $.extend(defaults, options);

        function initialize() {
            $('#lightbox').show();
            return false;
        };

        function close() {
            $('#lightbox').hide();
        };

        return this.click(initialize);
    };

})(jQuery);

I then use....

$('a.image').Lightbox();

to run it. I want to be able to call the close() function seperately e.g.

$.Lightbox.close();

How can I achieve this within my code?

+4  A: 

You can add a method for that, like this:

(function($) {
    $.fn.Lightbox = function(options) {
       var opts = $.extend(defaults, options);

        function initialize() {
            $('#lightbox').show();
            return false;
        };

        function close() {
            $('#lightbox').hide();
        };
        return this.click(initialize);
    };    
    $.Lightbox = {
        close: function() {
            $('#lightbox').hide();
        }
    };
})(jQuery);
Nick Craver
Thanks, is there any way I can get it to call the original close() function without repeating the code?
fire
@fire - I'd do the reverse, tying that one to the main: `function close() { $.Lightbox.close(); } `...since there's nothing instance dependent on there, might as well keep it simple.
Nick Craver