Tag Archives: Windows games

clickoutside jQuery plugin

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.