Skip to content

Instantly share code, notes, and snippets.

@bessfernandez
Last active April 9, 2019 16:42
Show Gist options
  • Select an option

  • Save bessfernandez/c6edbe2c19890d1568b31720fa867908 to your computer and use it in GitHub Desktop.

Select an option

Save bessfernandez/c6edbe2c19890d1568b31720fa867908 to your computer and use it in GitHub Desktop.
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))
@bessfernandez
Copy link
Copy Markdown
Author

bessfernandez commented Apr 9, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment