Last active
April 9, 2019 16:42
-
-
Save bessfernandez/c6edbe2c19890d1568b31720fa867908 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var root = document.getElementById('root'), | |
domSearch = function (root, className) { | |
var stack = [root], | |
foundNodes = []; | |
while (stack.length > 0) { | |
var nodeToCheck = stack.pop(); | |
if (nodeToCheck.children.length > 0) { | |
for (var i = 0; i < nodeToCheck.children.length; i++) { | |
stack.push(nodeToCheck.children[i]); | |
} | |
} | |
if (nodeToCheck.className === className) { | |
foundNodes.push(nodeToCheck); | |
} | |
} | |
return foundNodes; | |
}; | |
let foundNodes = domSearch(root, 'a'); | |
console.log(foundNodes.map(item => item.className)) | |
selectorSearch = function (root, selector) { | |
let foundNodes = []; | |
let selectorMatch = root.querySelectorAll(selector); | |
if (selectorMatch.length) { | |
for (var i = 0; i < selectorMatch.length; i++) { | |
foundNodes.push(selectorMatch[i]); | |
} | |
} | |
return foundNodes; | |
}; | |
let foundSelectorNodes = selectorSearch(root, '.a > .b >.c'); | |
console.log(foundSelectorNodes.map(item => item.innerHTML)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is an example of using Queues or whatever to traverse through the DOM and find matching nodes for both a specific class name and
querySelectorAll
for a specific yup query