Bug Tracker

Modify

Ticket #4072 (closed enhancement: fixed)

Opened 4 years ago

Last modified 4 years ago

Provide a selector/node restriction option as a second argument to .closest

Reported by: dantman Owned by:
Priority: major Milestone: 1.4
Component: core Version: 1.3.2
Keywords: Cc:
Blocking: Blocked by:

Description

A second argument to .closest to restrict how far the backwards traversing through the dom can go would be useful.

This would allow people to write something like: $(#foo').click(function(e) {

var elm = $(e.target).closest('li', this); ...

});

In a case like this document: <ul>

<li>

<ul id=foo>

<li>...</li>

</ul>

</li>

</ul>

If someone clicked directly on the ul#foo instead of the li (this is possible after the list has been styled) then the current .closest would falsely return the li which is a parent of the ul. Effectively using "this" as the restriction makes it so that tracing back through the dom never goes past the node we put the even on.

Ideally the restriction could be specified using a dom node, jQuery object, or selector (which would be matched as we walk along rather than traversing itself).

Change History

comment:1 Changed 4 years ago by joern

Related (with attachment): #4945

comment:2 Changed 4 years ago by john

  • Status changed from new to closed
  • Resolution set to fixed
  • Version changed from 1.3.1 to 1.3.2
  • Component changed from unfilled to core
  • Milestone changed from 1.3.2 to 1.3.3

Fixed in SVN rev [6507]. Implemented support for .context-limited .closest() method.

comment:3 Changed 4 years ago by john

Ok, context-limited .closest() didn't work as intended. Implemented .closest(filter, DOMElement) instead. Landed in SVN rev [6520].

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.