Bind Show Hide Events With jQuery

For the chaining rule replace ‘el.apply(this, arguments);’ with “return el.apply(this, arguments);”

Maxime Parmentier

Here is a little snippet to allow you to bind events when dom elements are shown hidden with jQuery.

(function ($) {
  $.each(['show', 'hide'], function (i, ev) {
    var el = $.fn[ev];
    $.fn[ev] = function () {
      this.trigger(ev);
      el.apply(this, arguments);
    };
  });
})(jQuery);

And now bind the event on a specific dom element:

$(function () {
  $('div').on('hide', function () {
    alert('div hidden');
  });
});

View original post

Advertisements