I wrote a small plugin to jQuery that adds the event of clicking outside an element. You know sometimes you should close some modal windows when clicking outside. It’s really simple and small. Please provide any feedback that will improve it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | (function(jQuery) { jQuery.fn.clickoutside = function(callback) { var outside = 1, self = $(this); self.cb = callback; this.click(function() { outside = 0; }); $(document).click(function() { outside && self.cb(); outside = 1; }); return $(this); } })(jQuery); |
You can see the demo page here. To install it just include the chunk above after the jQuery library code.
Related posts:
- Event driven programming with jQuery – (part 3). What is a module?
- jQuery tooltip plugin!
- jQuery localStorage plugin




You did great job!
yeah, there are plenty of plugins out there!
vui la chinh
very small and do what it has to. leave it as it is
Thanks!