src code

class method Event.findElement

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, the document itself (HTMLDocument node) is returned.*

Example

Here's a simple code 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(event, 'p');
  if (element != document)
    $(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.