jQuery Detect Out-of-Bounds Clicks with Event.target

In jQuery, event.target always refers to the element that triggered the event, where ‘event’ is the parameter passes to the function. event.target represents DOM object and is able to access all its properties or attributes that has a value.

Note also that this will also work, it represnts a DOM element rather than a jQuery object, so you must refer to it as $(this) when there is a jQuery function on it

It was incorrectly pointed out in the comments that e.target does not work in IE; this is NOT true as jQuery’s Event object fixes these inconsistencies where necessary (IE, Safari).

Here’s an extension to add a clickOutside function to elements:

With this you could easily do

Leave a Reply

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