class method Event.findElement

View source on GitHub →

Event.findElement(event[, expression]) → Element
  • event (Event) – An Event object
  • expression (String) – An optional CSS selector

Returns the first DOM element that matches a given CSS selector — starting with the element on which the event occurred, then moving up its ancestor chain. If expression is not given, the element which fired the event is returned.

If no matching element is found, undefined is returned.

Example

Here's a simple example that lets you click everywhere on the page and hides the closest-fitting paragraph around your click (if any).

document.observe('click', function(event) {
  var element = event.findElement('p');
  if (element)
    $(element).hide();
});

This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.