Re: Native DOM way to get nodes of arbitrary type/name

* Marat Tanalin wrote:
>It would be nice to have a native (usable and performant) DOM way for 
>retrieving DOM nodes by node type (or, alternatively, by node name).
>
>This could be represented by these two simple methods:
>
>    * element.getNodesByType(type) -- to get _all_ nodes
>      of specified type contained in the element
>      (like `element.getElementsByTagName('*')` for elements);

Use XPath, the convenient API would have it

  element.selectNodes('.//text()')
  element.selectNodes('.//comment()')
  element.selectNodes('.//processing-instruction()')

but in some browsers you have to use the DOM Level 3 XPath methods,
which are slightly less convenient. Note in particular that XPath
allows you to use predicates to select a subset of "all" the nodes
that would otherwise be returned.

>    * element.getChildNodesByType(type) -- to get _direct child_
>      nodes of the element that have the specified type
>      (like `element.children` for elements).

Drop the `.//` in the paths above for this.
-- 
Björn Höhrmann · mailto:bjoern@hoehrmann.de · http://bjoern.hoehrmann.de
Am Badedeich 7 · Telefon: +49(0)160/4415681 · http://www.bjoernsworld.de
25899 Dagebüll · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/ 

Received on Saturday, 5 October 2013 10:12:50 UTC