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?
asked Oct 15, 2010 at 14:16
fire
21.5k18 gold badges82 silver badges116 bronze badges
1 Answer 1
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);
answered Oct 15, 2010 at 14:24
Nick Craver
631k138 gold badges1.3k silver badges1.2k bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
fire
Thanks, is there any way I can get it to call the original close() function without repeating the code?
Nick Craver
@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.lang-js