#184 new
Luke Bunselmeyer

Event.findElement indicate wrong return value for unmatched selector

Reported by Luke Bunselmeyer | October 26th, 2011 @ 03:44 AM

Hi Folks,

The API docs for Event.findElement indicate that "If no matching element is found, the document itself (HTMLDocument node) is returned." However, it seems that if no matching element is found, then undefined is returned. Specifically, element.parentNode will eventually be null as the while loop climbs up the dom tree, the while loop will stop, and the method will return as undefined. Moreover, the unit tests indicate that null should be returned.

Personally, I'm hoping this is a documentation bug, because I have code in-place that assumes that undefined/null (aka falsy) is returned.


  function findElement(event, expression) {
    var element = Event.element(event);
    if (!expression) return element;
    while (element) {
      if (Object.isElement(element) && Prototype.Selector.match(element, expression)) {
        return Element.extend(element);
      element = element.parentNode;


testEventFindElement: function() {
    var span = $("span"), event;
    event = span.fire("test:somethingHappened");
    this.assertElementMatches(event.findElement(), 'span#span');
    this.assertElementMatches(event.findElement('span'), 'span#span');
    this.assertElementMatches(event.findElement('p'), 'p#inner');
    this.assertEqual(null, event.findElement('div.does_not_exist'));
    this.assertElementMatches(event.findElement('.does_not_exist, span'), 'span#span');

Comments and changes to this ticket

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Shared Ticket Bins

People watching this ticket