getElementsByTagNames()
By Peter-Paul Koch | January 30th, 2006 | Filed in DOM, DOM Scripting TF, W3C/Standards Documentation
getElementsByTagNames() returns elements with several tag names in the order they appear in the document.
Skip to comment formOne of my fondest W3C DOM wishes is a getElementsByTagNames()
method (note the plural “names”) that returns elements with several tag names in the order they appear in the document. This is extremely useful in for instance my ToC script which needs all h3s and h4s in the order they appear in the source code.
When I discovered the compareDocumentPosition()
method in Level 3 Core, I could finally write a custom script that works in most browsers.
Therefore I now proudly present my new getElementsByTagNames() script. It requires either sourceIndex
or compareDocumentPosition
to work fully, and since Safari 1.3.2 supports neither the script doesn’t sort the elements in this browser.