In jQuery, I know you can extend and create functions for certain types of operations through $.fn
.
So:
(function ( $ ) {
$.fn.clickOut = function (callback) {
var that = this;
$(document).click(function (e) {
if ( $(e.target).closest(that).size() > 0) {
return;
}
$.isFunction(callback) && $.proxy(callback, that)(e);
});
}
})( jQuery )
The function works correctly. But how can I make it work with on
, because I need to assign it to dynamically created elements.
Is there any way to "create a function" (I do not know how else to say this) to on
of jQuery
?
Roughly speaking, I wanted it to work like this:
$('div').clickOut(); // Forma normal
// Forma que tentei, e não deu certo.
$('#my-uls').on('clickOut', 'li', function (...){});