Bug 16488 - Consider adding parent() / ancestor() method
Summary: Consider adding parent() / ancestor() method
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
Depends on:
Reported: 2012-03-23 07:57 UTC by Anne
Modified: 2014-05-26 14:25 UTC (History)
8 users (show)

See Also:


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)
      return node
  return null

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

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