{"id":42779,"date":"2018-12-22T21:40:15","date_gmt":"2018-12-22T13:40:15","guid":{"rendered":"https:\/\/www.hongkiat.com\/blog\/?p=42779"},"modified":"2025-04-04T02:51:38","modified_gmt":"2025-04-03T18:51:38","slug":"filter-traverse-dom-tree-with-javascript","status":"publish","type":"post","link":"https:\/\/www.hongkiat.com\/blog\/filter-traverse-dom-tree-with-javascript\/","title":{"rendered":"How to Filter and Traverse DOM Tree with JavaScript"},"content":{"rendered":"<p>Did you know there\u2019s a JavaScript API whose sole mission is to <strong>filter out and iterate through the nodes<\/strong> we want from a DOM tree? In fact, not one but there are two such APIs: <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/NodeIterator\" target=\"_blank\" rel=\"noopener\"><code>NodeIterator<\/code><\/a> and <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/TreeWalker\" target=\"_blank\" rel=\"noopener\"><code>TreeWalker<\/code><\/a>. They\u2019re quite similar to one another, with some useful differences. Both can <strong>return a list of nodes<\/strong> that are present under a given root node while complying with <strong>any predefined and\/or custom filter rules<\/strong> applied to them.<\/p>\n<p>The predefined filters available in the APIs can help us <strong>target different kinds of nodes<\/strong> such as text nodes or element nodes, and custom filters (added by us) can <strong>further filter the bunch<\/strong>, for instance by looking for nodes with specific contents. The returned list of nodes are <strong>iterable<\/strong>, i.e. they can be <strong>looped through<\/strong>, and we can work with all the individual nodes in the list.<\/p>\n<p class=\"note\"><strong>Read Also:<\/strong> <a target=\"_blank\" href=\"https:\/\/www.hongkiat.com\/blog\/understanding-document-object-model\/\" rel=\"noopener\">Understanding Document Object Model (DOM) in Details<\/a><\/p>\n<h2>How to use the <code>NodeIterator<\/code> API<\/h2>\n<p>A <code>NodeIterator<\/code> object can be created using the <code><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Document\/createNodeIterator\" target=\"_blank\" rel=\"noopener\">createNodeIterator()<\/a><\/code> method of the <code>document<\/code> interface. This method <strong>takes three arguments<\/strong>. The first one is required; it\u201ds the <strong>root node<\/strong> that holds all the nodes we want to filter out.<\/p>\n<p>The second and third arguments are <strong>optional<\/strong>. They are the <strong>predefined and custom filters<\/strong>, respectively. The predefined filters are available for use as <strong>constants of the <code>NodeFilter<\/code> object<\/strong>.<\/p>\n<p>For example, if the <code>NodeFilter.SHOW_TEXT<\/code> constant is added as the second parameter it will return an iterator for a <strong>list of all the text nodes under the root node<\/strong>. <code>NodeFilter.SHOW_ELEMENT<\/code> will return <strong>only the element nodes<\/strong>. See a full list of <strong><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Document\/createNodeIterator#Values\" target=\"_blank\" rel=\"noopener\">all the available constants<\/a><\/strong>.<\/p>\n<p>The third argument (the custom filter) is a <strong>function that implements the filter<\/strong>.<\/p>\n<p>Here is an <strong>example code snippet<\/strong>:<\/p>\n<pre>\r\n&lt;!doctype html&gt;\r\n&lt;html lang='en'&gt;\r\n  &lt;head&gt;\r\n    &lt;meta charset='UTF-8'&gt;\r\n    &lt;title&gt;Document&lt;\/title&gt;\r\n  &lt;\/head&gt;\r\n  &lt;body&gt;\r\n    &lt;header&gt;&lt;h1&gt;title&lt;\/h1&gt;&lt;\/header&gt;\r\n    &lt;div id='wrapper'&gt;\r\n      this is the page wrapper\r\n      &lt;p&gt;Hello&lt;\/p&gt;\r\n      &lt;p&gt;How are you?&lt;\/p&gt;\r\n    &lt;\/div&gt;\r\n    &lt;span&gt;txt&lt;\/span&gt;\r\n    &lt;a href='#'&gt;some link&lt;\/a&gt;\r\n    &lt;footer&gt;copyrights&lt;\/footer&gt;\r\n  &lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>Assuming we want to <strong>extract the contents of all the text nodes that are inside the <code>#wrapper<\/code> div<\/strong>, this is how we go about it using <code>NodeIterator<\/code>:<\/p>\n<pre>\r\nvar div = document.querySelector('#wrapper');\r\nvar nodeIterator = document.createNodeIterator(\r\n  div,\r\n  NodeFilter.SHOW_TEXT\r\n);\r\nwhile(nodeIterator.nextNode()) {\r\n  console.log(nodeIterator.referenceNode.nodeValue.trim());\r\n}\r\n\/* console output\r\n[Log] this is the page wrapper\r\n[Log] Hello\r\n[Log]\r\n[Log] How are you?\r\n[Log]\r\n*\/\r\n<\/pre>\n<p>The <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/NodeIterator\/nextNode\" target=\"_blank\" rel=\"noopener\"><code>nextNode()<\/code><\/a> method of the <code>NodeIterator<\/code> API <strong>returns the next node in the list of iterable text nodes<\/strong>. When we use it in a <code>while<\/code> loop to access each node in the list, we log the trimmed contents of every text node into the console. The <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/NodeIterator\/referenceNode\" target=\"_blank\" rel=\"noopener\"><code>referenceNode<\/code><\/a> property of <code>NodeIterator<\/code> <strong>returns the node the iterator is currently attached to<\/strong>.<\/p>\n<p>As you can see in the output, there are some text nodes with just empty spaces for their contents. We can <strong>avoid showing these empty contents using a custom filter<\/strong>:<\/p>\n<pre>\r\nvar div = document.querySelector('#wrapper');\r\nvar nodeIterator = document.createNodeIterator(\r\n  div,\r\n  NodeFilter.SHOW_TEXT,\r\n  function(node) {\r\n    return (node.nodeValue.trim() !== \"\") ?\r\n    NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT;\r\n  }\r\n);\r\nwhile(nodeIterator.nextNode()) {\r\n  console.log(nodeIterator.referenceNode.nodeValue.trim());\r\n}\r\n\/* console output\r\n[Log] this is the page wrapper\r\n[Log] Hello\r\n[Log] How are you?\r\n*\/\r\n<\/pre>\n<p>The custom filter function <strong>returns the constant <code>NodeFilter.FILTER_ACCEPT<\/code>if the text node is not empty<\/strong>, which leads to the inclusion of that node in the list of nodes the iterator will be iterating over. Contrary, the <code>NodeFilter.FILTER_REJECT<\/code> constant is returned in order to <strong>exclude the empty text nodes<\/strong> from the iterable list of nodes.<\/p>\n<h2>How to use the <code>TreeWalker<\/code> API<\/h2>\n<p>As I mentioned before, the <code>NodeIterator<\/code> and <code>TreeWalker<\/code> APIs are <strong>similar to each other<\/strong>.<\/p>\n<p><code>TreeWalker<\/code> can be created using the <code><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Document\/createTreeWalker\" target=\"_blank\" rel=\"noopener\">createTreeWalker()<\/a><\/code> method of the <code>document<\/code> interface. This method, just like <code>createNodeFilter()<\/code>, <strong>takes three arguments<\/strong>: <strong>the root node, a predefined filter, and a custom filter<\/strong>.<\/p>\n<p>If we <strong>use the <code>TreeWalker<\/code> API instead of <code>NodeIterator<\/code><\/strong> the previous code snippet looks like the following:<\/p>\n<pre>\r\nvar div = document.querySelector('#wrapper');\r\nvar treeWalker = document.createTreeWalker(\r\n  div,\r\n  NodeFilter.SHOW_TEXT,\r\n  function(node) {\r\n    return (node.nodeValue.trim() !== \"\") ?\r\n    NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT;\r\n  }\r\n);\r\nwhile(treeWalker.nextNode()) {\r\n  console.log(treeWalker.currentNode.nodeValue.trim());\r\n}\r\n\/* output\r\n[Log] this is the page wrapper\r\n[Log] Hello\r\n[Log] How are you?\r\n*\/\r\n<\/pre>\n<p>Instead of <code>referenceNode<\/code>, the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/TreeWalker\/currentNode\" target=\"_blank\" rel=\"noopener\"><code>currentNode<\/code><\/a> property of the <code>TreeWalker<\/code> API is used to <strong>access the node to which the iterator is currently attached<\/strong>. In addition to the <code>nextNode()<\/code> method, <code>Treewalker<\/code> has other useful methods. The <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/TreeWalker\/previousNode\" target=\"_blank\" rel=\"noopener\"><code>previousNode()<\/code><\/a> method (also <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/NodeIterator\/previousNode\" target=\"_blank\" rel=\"noopener\">present in <code>NodeIterator<\/code><\/a>) <strong>returns the previous node<\/strong> of the node the iterator is currently anchored to.<\/p>\n<p>Similar functionality is performed by the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/TreeWalker\/parentNode\" target=\"_blank\" rel=\"noopener\"><code>parentNode()<\/code><\/a>, <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/TreeWalker\/firstchild\" target=\"_blank\" rel=\"noopener\"><code>firstChild()<\/code><\/a>, <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/TreeWalker\/lastchild\" target=\"_blank\" rel=\"noopener\"><code>lastChild()<\/code><\/a>, <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/TreeWalker\/previoussibling\" target=\"_blank\" rel=\"noopener\"><code>previousSibling()<\/code><\/a>, and <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/TreeWalker\/nextsibling\" target=\"_blank\" rel=\"noopener\"><code>nextSibling()<\/code><\/a> methods. These methods are <strong>only available in the <code>TreeWalker<\/code> API<\/strong>.<\/p>\n<p>Here\u2019s a code example that <strong>outputs the last child of the node<\/strong> the iterator is anchored to:<\/p>\n<pre>\r\nvar div = document.querySelector('#wrapper');\r\n  var treeWalker = document.createTreeWalker(\r\n  div,\r\n  NodeFilter.SHOW_ELEMENT\r\n);\r\nconsole.log(treeWalker.lastChild());\r\n\/*  output\r\n[Log] &lt;p&gt;How are you?&lt;\/p&gt;\r\n*\/\r\n<\/pre>\n<h2>Which API to choose<\/h2>\n<p>Choose the <code>NodeIterator<\/code> API, when you <strong>need just a simple iterator<\/strong> to filter and loop through the selected nodes. And, pick the <code>TreeWalker<\/code> API, when you <strong>need to access the filtered nodes\u2019 family<\/strong>, such as their immediate siblings.<\/p>\n<p class=\"note\"><strong>Read Also:<\/strong> <a target=\"_blank\" href=\"https:\/\/www.hongkiat.com\/blog\/dom-manipulation-javascript-methods\/\" rel=\"noopener\">15 JavaScript Methods For DOM Manipulation for Web Developers<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>Did you know there\u2019s a JavaScript API whose sole mission is to filter out and iterate through the nodes we want from a DOM tree? In fact, not one but there are two such APIs: NodeIterator and TreeWalker. They\u2019re quite similar to one another, with some useful differences. Both can return a list of nodes&hellip;<\/p>\n","protected":false},"author":145,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3392],"tags":[3554,4117],"topic":[4520],"class_list":["entry-content","is-maxi"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v22.8 (Yoast SEO v27.4) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>How to Filter and Traverse DOM Tree with JavaScript - Hongkiat<\/title>\n<meta name=\"description\" content=\"Did you know there&#039;s a JavaScript API whose sole mission is to filter out and iterate through the nodes we want from a DOM tree? In fact, not one but\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.hongkiat.com\/blog\/filter-traverse-dom-tree-with-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Filter and Traverse DOM Tree with JavaScript\" \/>\n<meta property=\"og:description\" content=\"Did you know there&#039;s a JavaScript API whose sole mission is to filter out and iterate through the nodes we want from a DOM tree? In fact, not one but\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.hongkiat.com\/blog\/filter-traverse-dom-tree-with-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"Hongkiat\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/hongkiatcom\" \/>\n<meta property=\"article:published_time\" content=\"2018-12-22T13:40:15+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-04-03T18:51:38+00:00\" \/>\n<meta name=\"author\" content=\"Preethi Ranjit\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:site\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Preethi Ranjit\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/filter-traverse-dom-tree-with-javascript\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/filter-traverse-dom-tree-with-javascript\\\/\"},\"author\":{\"name\":\"Preethi Ranjit\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/e981676afae36d1ff5feb75094950ab3\"},\"headline\":\"How to Filter and Traverse DOM Tree with JavaScript\",\"datePublished\":\"2018-12-22T13:40:15+00:00\",\"dateModified\":\"2025-04-03T18:51:38+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/filter-traverse-dom-tree-with-javascript\\\/\"},\"wordCount\":639,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"keywords\":[\"DOM\",\"Javascripts\"],\"articleSection\":[\"Coding\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/filter-traverse-dom-tree-with-javascript\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/filter-traverse-dom-tree-with-javascript\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/filter-traverse-dom-tree-with-javascript\\\/\",\"name\":\"How to Filter and Traverse DOM Tree with JavaScript - Hongkiat\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\"},\"datePublished\":\"2018-12-22T13:40:15+00:00\",\"dateModified\":\"2025-04-03T18:51:38+00:00\",\"description\":\"Did you know there's a JavaScript API whose sole mission is to filter out and iterate through the nodes we want from a DOM tree? In fact, not one but\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/filter-traverse-dom-tree-with-javascript\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/filter-traverse-dom-tree-with-javascript\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/filter-traverse-dom-tree-with-javascript\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Filter and Traverse DOM Tree with JavaScript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"name\":\"Hongkiat\",\"description\":\"Tech and Design Tips\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\",\"name\":\"Hongkiat.com\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"contentUrl\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"width\":1200,\"height\":799,\"caption\":\"Hongkiat.com\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/hongkiatcom\",\"https:\\\/\\\/x.com\\\/hongkiat\",\"https:\\\/\\\/www.pinterest.com\\\/hongkiat\\\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/e981676afae36d1ff5feb75094950ab3\",\"name\":\"Preethi Ranjit\",\"description\":\"A .NET developer with a JavaScript background, Preethi is expert in front-end coding, JavaScript, HTML, and CSS.\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/author\\\/preethi\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to Filter and Traverse DOM Tree with JavaScript - Hongkiat","description":"Did you know there's a JavaScript API whose sole mission is to filter out and iterate through the nodes we want from a DOM tree? In fact, not one but","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.hongkiat.com\/blog\/filter-traverse-dom-tree-with-javascript\/","og_locale":"en_US","og_type":"article","og_title":"How to Filter and Traverse DOM Tree with JavaScript","og_description":"Did you know there's a JavaScript API whose sole mission is to filter out and iterate through the nodes we want from a DOM tree? In fact, not one but","og_url":"https:\/\/www.hongkiat.com\/blog\/filter-traverse-dom-tree-with-javascript\/","og_site_name":"Hongkiat","article_publisher":"https:\/\/www.facebook.com\/hongkiatcom","article_published_time":"2018-12-22T13:40:15+00:00","article_modified_time":"2025-04-03T18:51:38+00:00","author":"Preethi Ranjit","twitter_card":"summary_large_image","twitter_creator":"@hongkiat","twitter_site":"@hongkiat","twitter_misc":{"Written by":"Preethi Ranjit","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.hongkiat.com\/blog\/filter-traverse-dom-tree-with-javascript\/#article","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/filter-traverse-dom-tree-with-javascript\/"},"author":{"name":"Preethi Ranjit","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/e981676afae36d1ff5feb75094950ab3"},"headline":"How to Filter and Traverse DOM Tree with JavaScript","datePublished":"2018-12-22T13:40:15+00:00","dateModified":"2025-04-03T18:51:38+00:00","mainEntityOfPage":{"@id":"https:\/\/www.hongkiat.com\/blog\/filter-traverse-dom-tree-with-javascript\/"},"wordCount":639,"commentCount":0,"publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"keywords":["DOM","Javascripts"],"articleSection":["Coding"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.hongkiat.com\/blog\/filter-traverse-dom-tree-with-javascript\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.hongkiat.com\/blog\/filter-traverse-dom-tree-with-javascript\/","url":"https:\/\/www.hongkiat.com\/blog\/filter-traverse-dom-tree-with-javascript\/","name":"How to Filter and Traverse DOM Tree with JavaScript - Hongkiat","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/#website"},"datePublished":"2018-12-22T13:40:15+00:00","dateModified":"2025-04-03T18:51:38+00:00","description":"Did you know there's a JavaScript API whose sole mission is to filter out and iterate through the nodes we want from a DOM tree? In fact, not one but","breadcrumb":{"@id":"https:\/\/www.hongkiat.com\/blog\/filter-traverse-dom-tree-with-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.hongkiat.com\/blog\/filter-traverse-dom-tree-with-javascript\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.hongkiat.com\/blog\/filter-traverse-dom-tree-with-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.hongkiat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to Filter and Traverse DOM Tree with JavaScript"}]},{"@type":"WebSite","@id":"https:\/\/www.hongkiat.com\/blog\/#website","url":"https:\/\/www.hongkiat.com\/blog\/","name":"Hongkiat","description":"Tech and Design Tips","publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.hongkiat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.hongkiat.com\/blog\/#organization","name":"Hongkiat.com","url":"https:\/\/www.hongkiat.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","contentUrl":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","width":1200,"height":799,"caption":"Hongkiat.com"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/hongkiatcom","https:\/\/x.com\/hongkiat","https:\/\/www.pinterest.com\/hongkiat\/"]},{"@type":"Person","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/e981676afae36d1ff5feb75094950ab3","name":"Preethi Ranjit","description":"A .NET developer with a JavaScript background, Preethi is expert in front-end coding, JavaScript, HTML, and CSS.","url":"https:\/\/www.hongkiat.com\/blog\/author\/preethi\/"}]}},"jetpack_featured_media_url":"https:\/\/","jetpack_shortlink":"https:\/\/wp.me\/p4uxU-b7Z","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/42779","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/users\/145"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/comments?post=42779"}],"version-history":[{"count":3,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/42779\/revisions"}],"predecessor-version":[{"id":73743,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/42779\/revisions\/73743"}],"wp:attachment":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/media?parent=42779"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/categories?post=42779"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/tags?post=42779"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/topic?post=42779"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}