title: Attaching jQuery event handlers so that they are triggered first
body: <p>Is there a way to attach a jQuery event handler such that the handler is triggered before any previously-attached event handlers?  I came across <a href="http://shchekoldin.com/2010/11/18/reorder-jquery-event-handlers/" rel="noreferrer">this article</a>, but the code didn't work because event handlers are no-longer stored in an array, which is what his code expected.  I attempted to create a jQuery extension to do what I wanted, but this is not working (the events still fire in the order they were bound):</p>
  
  <pre><code>$.fn.extend({
      bindFirst: function(type, handler) {
  
          var baseType = type;
          var dotIdx = type.indexOf('.');
          if (dotIdx &gt;= 0) {
              baseType = type.substr(0, dotIdx);
          }
  
          this.each(function() {
              var oldEvts = {};
              var data = $.data(this);
              var events = data.events || data.__events__;
              var handlers = events[baseType];
              for (var h in handlers) {
                  if (handlers.hasOwnProperty(h)) {
                      oldEvts[h] = handlers[h];
                      delete handlers[h];
                      // Also tried an unbind here, to no avail
                  }
              }
  
              var self = $(this);
              self.bind(type, handler);
  
              for (var h in oldEvts) {
                  if (oldEvts.hasOwnProperty(h)) {
                      self.bind(baseType, oldEvts[h]);
                  }
              }
          });
      }
  });
  </code></pre>
  
  <p>Is there a <em>natural</em> way to reorder event handling?  If there isn't, do you know of technique I could apply?  I'm using jQuery 1.4.1, though I'll upgrade if I must.</p>