Skip to content

Instantly share code, notes, and snippets.

@mkdizajn
Last active September 8, 2017 11:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkdizajn/9190429eb5e50e85e98c67d0de27cd82 to your computer and use it in GitHub Desktop.
Save mkdizajn/9190429eb5e50e85e98c67d0de27cd82 to your computer and use it in GitHub Desktop.
How easily to hook into native jQuery event,, I have built example to alter attr method, but it can easily be 'addClass', 'removeClass' etc..
(function($) {
// save Original attr method!
var orgAttr = $.fn.attr;
// Change attr | addClass | similar method to be able to hook to JQ event
$.fn.attr = function(){
var result = orgAttr.apply( this, arguments ); // Execute the original method.
$(this).trigger('customTriggerName'); // trigger custom event on element
return result; // return the original result
}
// HOOK @ ATTR binded event
$('.element').bind('customTriggerName', function(event){
// do some custom stuff here!
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment