JasonDaly.name

PHP, Ruby, Symfony, Rails, Doctrine, MooTools. Web Development.
August 4, 2011

MooTools focusedKeyUp Event

Recently needed to apply a keyup event to a series of textfields during MooTools domready event, but only have each field’s event execute when that field has the user’s focus.

/**
 * Custom MooTools event, restricting firing of the keyUp event to only when the target element has focus.
 *
 * Usage:
 *   some_element.addEvent('focusedKeyUp', function(event) {
 *     console.debug('A key was pressed while the following element has focus: ' + event.target.get('id'));
 *   });
 */
Element.Events.focusedKeyUp = {
  base: 'keyup',
  condition: function(event) {
    return event.target == $$(event.target.get('tag') + ':focus')[0];
  }
};

(Source: gist.github.com)

Tags: Mootools javascript code