clickoutside jQuery plugin

submit to reddit

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:

  1. Event driven programming with jQuery – (part 3). What is a module?
  2. jQuery tooltip plugin!
  3. jQuery localStorage plugin

You are a GREAT developer? Click here to answer the weekly quiz!

This entry was posted in javascript and tagged , , , , , , , . Bookmark the permalink.

5 Responses to clickoutside jQuery plugin

  1. dass says:

    You did great job!

  2. dikram says:

    yeah, there are plenty of plugins out there!

  3. ha ngoc yen says:

    vui la chinh

  4. j23d says:

    very small and do what it has to. leave it as it is ;)

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">