Bug Tracker

Ticket #10449 (closed enhancement: fixed)

Opened 4 years ago

Last modified 4 years ago

Function $("#id").closest(".class") returns element $("#id") itself if it has .class

Reported by: San4es Owned by: kswedberg
Priority: low Milestone: 1.7
Component: traversing Version: 1.7b1
Keywords: Cc:
Blocking: Blocked by:


Tests were performed on following example with all popular browsers and all versions of jQuery:

<ul class="list">
    <li class="item">
        <ul class="list">
            <li id="test" class="item">2</li>            

( http://jsfiddle.net/QParz/1/)

When use $("#test").closest(".item") we get second list item, but it's expected first list item.

Change History

comment:1 Changed 4 years ago by San4es

In other point of view, that isn't bug if you using function .closest() considering semantic meaning of "closest" word, but documentation says "Get the first ancestor element..." and so element itself is unexpected value, because element is not equal to its ancestor, is it?

comment:2 Changed 4 years ago by ajpiano

  • Status changed from new to assigned
  • Component changed from unfiled to traversing
  • Priority changed from undecided to low
  • Owner set to kswedberg
  • Milestone changed from None to 1.7
  • Keywords needsdocs added
  • Type changed from bug to enhancement

I agree that the doc here could use a touch of refinement, but this isn't a bug - it's the way that closest has always worked since it was introduced and it is used thusly by both users and jQuery core internally for event delegation.

comment:3 Changed 4 years ago by timmywil

Docs seem pretty clear to me if one keeps reading.

comment:4 Changed 4 years ago by ajpiano

@timmywil, I agree - but the slug is still a touch on the unclear side

comment:5 Changed 4 years ago by dmethvin

  • Keywords needsdocs removed

I've updated the docs.

comment:6 Changed 4 years ago by dmethvin

  • Status changed from assigned to closed
  • Resolution set to fixed
Note: See TracTickets for help on using tickets.