-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?

1 Answer 1

5

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, is there any way I can get it to call the original close() function without repeating the code?
@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.

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.