Bug Tracker

Modify

Ticket #7612 (closed bug: invalid)

Opened 3 years ago

Last modified 3 years ago

Abnormal closest() behaviour

Reported by: blablagum@… Owned by:
Priority: undecided Milestone: 1.5
Component: unfiled Version: 1.4.4
Keywords: Cc:
Blocking: Blocked by:

Description

Let's say I have that basic html list :

    <ul id="target">
        <li>item a</li>
        <li>item b
            <ul id="source">
                <li>sub item a</li>
                <li>sub item b</li>
                <li>sub item c</li>
            </ul>
        </li>
        <li>item c</li>
    </ul>

I want to point to the element <ul id="target"> using the element <ul id="source">.

I do :

 $("#source").closest("ul"); // return the element <ul id="source">

It point on itself where it should return the element <UL id="target">

If I use :

$("#source").closest("ul#target"); // return <ul id="target">

It works as expected.

Change History

comment:1 Changed 3 years ago by jitter

  • Status changed from new to closed
  • Resolution set to invalid

Thanks for taking the time to contribute to the jQuery project by writing a bug report!

The documentation on  closest() says:

Get the first ancestor element that matches the selector, beginning at the current element and progressing up through the DOM tree.

So this is the expected behavior maybe you intended to use parents() which doesn't consider the current element as potential match. The documentation on closest() includes a list of the differences between these similar methods and even a sample for the exact behavior you experienced

Last edited 3 years ago by jitter (previous) (diff)

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.