Bug Tracker

Opened 12 years ago

Closed 12 years ago

#7612 closed bug (invalid)

Abnormal closest() behaviour

Reported by: [email protected] Owned by:
Priority: undecided Milestone: 1.5
Component: unfiled Version: 1.4.4
Keywords: Cc:
Blocked by: Blocking:


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>
        <li>item c</li>

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 (1)

comment:1 Changed 12 years ago by jitter

Resolution: invalid
Status: newclosed

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 12 years ago by jitter (previous) (diff)
Note: See TracTickets for help on using tickets.