Skip to content

Instantly share code, notes, and snippets.

@tasdemirbahadir
Last active January 19, 2017 07:49
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 tasdemirbahadir/d09cee64a499d80f06e125b8664b7659 to your computer and use it in GitHub Desktop.
Save tasdemirbahadir/d09cee64a499d80f06e125b8664b7659 to your computer and use it in GitHub Desktop.
jQuery bindFirst Method, source: http://stackoverflow.com/a/2641047/1201725
// source: http://stackoverflow.com/a/2641047/1201725
//
// [name] is the name of the event "click", "mouseover", ..
// same as you'd pass it to bind()
// [fn] is the handler function
$.fn.bindFirst = function(name, fn) {
// bind as you normally would
// don't want to miss out on any jQuery magic
this.on(name, fn);
// namespaced events too.
this.each(function() {
var handlers = $._data(this, 'events')[name.split('.')[0]];
// take out the handler we just inserted from the end
var handler = handlers.pop();
// move it at the beginning
handlers.splice(0, 0, handler);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment