Bug 16488 - Consider adding parent() / ancestor() method
Summary: Consider adding parent() / ancestor() method
Status: RESOLVED FIXED
Alias: None
Product: WebAppsWG
Classification: Unclassified
Component: DOM (show other bugs)
Version: unspecified
Hardware: PC Windows 3.1
: P2 normal
Target Milestone: ---
Assignee: Anne
QA Contact: public-webapps-bugzilla
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-03-23 07:57 UTC by Anne
Modified: 2014-05-26 14:25 UTC (History)
8 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Anne 2012-03-23 07:57:36 UTC
The method either takes a function or a selector. While walking up the parent chain from the node on which the method was invoked, If the function returns true or the selector matches for the current node, it is returned, or null otherwise.

Suggestion from Ojan.
Comment 1 Anne 2012-11-21 18:45:43 UTC
Lachy, this might be something you're working on?
Comment 2 Anne 2012-11-29 11:47:34 UTC
Okay, so basically this is the method that is desired:

Element.prototype.ancestor = function(selector) {
  var node = this
  while((node = node.parent) && node.nodeType == Node.ELEMENT_NODE)
    if(node.matches(selector))
      return node
  return null
}

Correct?
Comment 3 David Håsäther 2013-06-17 10:11:22 UTC
Anne, I think a method that returns the element itself or an ancestor (what the specification calls an _inclusive ancestor_) would be more useful. This is typically what you want when doing event delegation.

The method name inclusiveAncestor() is not optimal though, so it would need some thinking.
Comment 4 David Håsäther 2014-05-22 15:03:32 UTC
jQuery and Dojo calls this closest(): http://api.jquery.com/closest/> and http://dojotoolkit.org/reference-guide/1.9/dojo/NodeList-traverse.html#closest

Tab also suggested closest() a while ago: http://lists.w3.org/Archives/Public/www-style/2014Jan/0051.html

It's not a verb, but neither is before() and after() for example.
Comment 5 Anne 2014-05-26 14:25:48 UTC
https://github.com/whatwg/dom/commit/fd69cc7ed9251d12b1185b24e6f89d3f0481f39d

Thanks David, hope you're enjoying your time off :-)