Side navigation
#4345 closed feature (fixed)
Opened March 13, 2009 04:56AM UTC
Closed November 05, 2010 02:25AM UTC
Last modified March 14, 2012 01:53PM UTC
add isDescendantOf and isAncestorOf
Reported by: | ap | Owned by: | |
---|---|---|---|
Priority: | low | Milestone: | |
Component: | selector | Version: | 1.3.2 |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
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.
Attachments (0)
Change History (4)
Changed March 14, 2009 12:42AM UTC by comment:1
Changed March 14, 2009 02:04AM UTC by comment:2
Also see this epic thread from jQuery-Dev:
http://groups.google.com/group/jquery-dev/browse_frm/thread/ac5ca8eaa64fe9f1/8b3cfed47617cb5d
Changed October 29, 2010 05:12PM UTC by comment:3
component: | core → selector |
---|---|
keywords: | isancestor isancestorof isdescendant isdescendantof ischild ischildof isparent isparentof → needsreview |
milestone: | 1.4 |
priority: | minor → low |
status: | new → open |
Bringing this back for review
Changed November 05, 2010 02:25AM UTC by comment:4
keywords: | needsreview |
---|---|
resolution: | → fixed |
status: | open → closed |
Related to #4101.