Skip to content

Instantly share code, notes, and snippets.

@natew
Created October 16, 2012 06:28
Show Gist options
  • Save natew/3897528 to your computer and use it in GitHub Desktop.
Save natew/3897528 to your computer and use it in GitHub Desktop.
jQuery function to mass bind events based on a parent
$.fn.allOn = function(onEvent, bindings) {
for (var target in bindings) {
(function(t) {
$(this).on(onEvent, t, function(e) {
bindings[t].call(this, e, $(this));
});
})(target);
}
}
@natew
Copy link
Author

natew commented Oct 16, 2012

Example usage:

body.allOn('click', {
  '.disabled': function() {
    return false;
  },

  '.control': function(e) {
  },

  '.restricted': function() {
    if (!isOnline) {
      modal('#modal-login');
      return false;
    }
  },

  '.link': function(e, el) {
    doSomething();
  }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment