:first selector behaves inconsistently in chained find() invocations
|Reported by:||hallettj||Owned by:|
Description (last modified by )
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 beta under Ubuntu 11.04.
Steps to reproduce:
- Create HTML structure with multiple elements, such as a list.
- Construct a jQuery selector that selects some or all of those elements, such as $('li').
- 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').
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.
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:
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 (7)
comment:1 follow-up: 2 Changed 6 years ago by
|Component:||unfiled → selector|
|Priority:||undecided → low|
|Status:||new → closed|
comment:6 Changed 6 years ago by