Bug Tracker

Opened 11 years ago

Last modified 11 years ago

#9747 closed bug

:first selector behaves inconsistently in chained find() invocations — at Initial Version

Reported by: hallettj Owned by:
Priority: low Milestone: 1.next
Component: selector Version: 1.6.1
Keywords: Cc:
Blocked by: Blocking:


Affects every version of jQuery that I tested: 1.2.6, 1.3.2, 1.4.4, 1.5.2, 1.6.1.

Reproduced in Chrome 13.0.782.32 beta under Ubuntu 11.04.

Steps to reproduce:

  1. Create HTML structure with multiple elements, such as a list.
  2. Construct a jQuery selector that selects some or all of those elements, such as $('li').
  3. Use a find() invocation on the previously selected elements with a selector that matches some nested element and include the expression ":first" in that selector. For example, $('li').find('a:first').

Expected results:

The first matched nested element is returned from the find() invocation. In the case of a list where nested elements are single anchor tags, the anchor tag in the first list element is returned.

Actual results:

Every nested element matching the given selectors is returned. It is as though ":first" has no effect on result of the find() invocation.

jsfiddle test case: http://jsfiddle.net/hallettj/q9zfd/1/

If I combine both selectors into one jQuery() call instead of using a chained find() invocation then the result is as I expect. For example, $('li a:first') returns a single anchor tag: the one in the first list item. But $('li').find('a:first') returns every anchor tag in every list item - assuming that each list item has at most one anchor tag.

Maybe this is intended behavior. But it is really not what I expected. If this is intended behavior it would be helpful to have a note in the documentation for :first, :eq, or find() that explains the discrepancy.

Change History (0)

Note: See TracTickets for help on using tickets.