Last Updated: February 25, 2016
·
1.057K
· glebm

jQuery prependEvent in 5 lines of coffeescript

Works on jQuery 1.9.

$.fn.prebind = (name, handler) ->
  @on name, handler
  @each ->
    handlers = $._data(@, 'events')[name.split('.')[0]]
    # move to the beginning
    handlers.splice 0, 0, handlers.pop()

This uses internal API ($._data) and might break in the future versions of jQuery

Adapted from http://stackoverflow.com/questions/2360655/jquery-event-handlers-always-execute-in-order-they-were-bound-any-way-around-t

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside