oncode
6/3/2014 - 10:10 AM

Overwrite jQuery 'addClass', 'toggleClass' and 'removeClass' methods to execute triggers when called.

Overwrite jQuery 'addClass', 'toggleClass' and 'removeClass' methods to execute triggers when called.

(function() {
  var methods = ['addClass', 'toggleClass', 'removeClass'];

  // extend jQuery methods to call a trigger
  $.map(methods, function(method) {
    // store the original handler function
    var originalMethod = $.fn[method];

    $.fn[method] = function() {
      // execute the original handler
      var result = originalMethod.apply(this, arguments);

      // trigger the custom event with arguments
      this.trigger(method, arguments);

      return result;
    };
  });
})();