Skip to main content

Bug Tracker

Side navigation

#7612 closed bug (invalid)

Opened November 24, 2010 09:14AM UTC

Closed November 24, 2010 10:09AM UTC

Abnormal closest() behaviour

Reported by: blablagum@hotmail.com Owned by:
Priority: undecided Milestone: 1.5
Component: unfiled Version: 1.4.4
Keywords: Cc:
Blocked by: Blocking:
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.

Attachments (0)
Change History (1)

Changed November 24, 2010 10:09AM UTC by jitter comment:1

_comment0: Thanks for taking the time to contribute to the jQuery project by writing a bug report! \ \ The documentation on [http://api.jquery.com/closest/ 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.1290593485059903
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