Last active
January 19, 2017 07:49
-
-
Save tasdemirbahadir/d09cee64a499d80f06e125b8664b7659 to your computer and use it in GitHub Desktop.
jQuery bindFirst Method, source: http://stackoverflow.com/a/2641047/1201725
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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