https://github.com/prettydiff/getnodesbytype
a custom DOM method to get nodes by node type
https://github.com/prettydiff/getnodesbytype
Last synced: 2 months ago
JSON representation
a custom DOM method to get nodes by node type
- Host: GitHub
- URL: https://github.com/prettydiff/getnodesbytype
- Owner: prettydiff
- License: mit
- Created: 2015-04-03T20:04:18.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2016-12-07T21:10:41.000Z (over 8 years ago)
- Last Synced: 2025-04-09T01:36:48.173Z (3 months ago)
- Language: JavaScript
- Size: 7.81 KB
- Stars: 19
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# getNodesByType
a custom DOM method to get nodes by node typeWhen the code is run a method `getNodesByType` will be attached to all element node types in the current DOM tree and the document object.
##Examples
###getNodesByType
Execute the method with either a numeric value or a node type name.document.getNodesByType(1); // get all element nodes in the document
document.getNodesByType("TEXT_NODE"); // get all text nodes in the documentvar item = document.getElementById("item");
item.getNodesByType("COMMENT_NODE"); // get all child comment node types
item.getNodesByType(1); // get all child element node types###getElementsByAttribute
Updated with a new method: `getElementsByAttribute(name, value)`. Both the name and value arguments of `getElementsByAttribute` are optional.document.getElementsByAttribute("class", "segment"); // get all elements with a class attribute of value segment
document.getElementsByAttribute("src", ""); // get all elements with a src attribute of any valuevar item = document.getElementById("item");
item.getElementsByAttribute("id"); // get all child elements with an id attribute of any value
item.getElementsByAttribute("", "purple_alien"); // get all child elements with any attribute whose value is purple_alien##Node Types
Here are the supported node types and their corresponding numeric values:* 0 - all nodes (non standard)
* 1 - "ELEMENT_NODE"
* 2 - "ATTRIBUTE_NODE"
* 3 - "TEXT_NODE"
* 4 - "CDATA_SECTION_NODE"
* 5 - "ENTITY_REFERENCE_NODE"
* 6 - "ENTITY_NODE"
* 7 - "PROCESSING_INSTRUCTION_NODE"
* 8 - "COMMENT_NODE"
* 9 - "DOCUMENT_NODE"
* 10 - "DOCUMENT_TYPE_NODE"
* 11 - "DOCUMENT_FRAGMENT_NODE"
* 12 - "NOTATION_NODE"All of the above node types remain valid types in the W3C specifications, but the WHATWG HTML5 group has deprecated these types:
* 2 - "ATTRIBUTE_NODE"
* 4 - "CDATA_SECTION_NODE"
* 5 - "ENTITY_REFERENCE_NODE"
* 6 - "ENTITY_NODE"
* 12 - "NOTATION_NODE"