Skip to content

Instantly share code, notes, and snippets.

@fabrizim
Forked from jonathanconway/jquery.prebind.js
Created October 17, 2015 01:29
Show Gist options
  • Save fabrizim/96cea76e0a8758c1ed0b to your computer and use it in GitHub Desktop.
Save fabrizim/96cea76e0a8758c1ed0b to your computer and use it in GitHub Desktop.
preBind() - Add an event binding *before* any pre-existing bindings. (An early version, may not work in all scenarios)
$.fn.preBind = function(type, data, fn) {
var currentBindings = this.data('events')[type];
var currentBindingsLastIndex = currentBindings.length - 1;
var newBindings = [];
// bind the event
this.bind(type, data, fn);
// move the new event to the top of the array
newBindings.push(currentBindings[currentBindingsLastIndex]);
$.each(currentBindings, function (index) {
if (index < currentBindingsLastIndex)
newBindings.push(this);
});
this.data('events')[type] = newBindings;
return this;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment