Bug Tracker

Modify

Ticket #4345 (closed feature: fixed)

Opened 4 years ago

Last modified 15 months ago

add isDescendantOf and isAncestorOf

Reported by: ap Owned by:
Priority: low Milestone:
Component: selector Version: 1.3.2
Keywords: Cc:
Blocking: Blocked by:

Description

It would be handy to test if an element is a parent/child of another element. This is particularly useful when handling event bubbling to determine if an event has already been handled (this is the most reliable technique I have found to date that works in most modern browsers).

I expect it would look something like this:

jQuery('#blah').isAncestorOf(someElement) and jQuery('#blah').isDescendantOf(someElement)

Alternatively but less accurately it could be called isParentOf/isChildOf.

My existing JS implementation runs in logarithmic time and is as follows:

/ test if a is a descendant of b */ function isDescendantOf(a, b) {

return isAncestorOf(b, a);

}

/ test if a is an ancestor of b */ function isAncestorOf(a, b) {

test if a is in b's parent chain var o = b; while (o) {

if (o === a)

return true;

o = o.parentNode; }

return false;

}

Having this as part of jQuery would be convenient.

Change History

comment:1 Changed 4 years ago by dmethvin

Related to #4101.

comment:3 Changed 3 years ago by rwaldron

  • Keywords needsreview added; isancestor isancestorof isdescendant isdescendantof ischild ischildof isparent isparentof removed
  • Priority changed from minor to low
  • Status changed from new to open
  • Component changed from core to selector
  • Milestone 1.4 deleted

Bringing this back for review

comment:4 Changed 3 years ago by dmethvin

  • Keywords needsreview removed
  • Status changed from open to closed
  • Resolution set to fixed

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.