Skip to content

Instantly share code, notes, and snippets.

@vneri
Last active December 29, 2021 15:37
Show Gist options
  • Save vneri/9c72a3a669c60d58181d1f01ecf972e7 to your computer and use it in GitHub Desktop.
Save vneri/9c72a3a669c60d58181d1f01ecf972e7 to your computer and use it in GitHub Desktop.
JavaScript querySelectorAll for all children and shadowRoot elements
function deepQuerySelectorAll(actual, selector){
var results = [];
// search for children
if (actual.shadowRoot != null){
deepQuerySelectorAll(actual.shadowRoot, selector).forEach( el => results.push(el));
}
if (actual.children !=null){
Array.from(actual.children).forEach(
child => {
deepQuerySelectorAll(child, selector).forEach(el => results.push(el));
}
);
}
// if we are finished, handle the parent
if (results.length == 0){
var foundElements = actual.querySelectorAll(selector);
if (foundElements != null){
Array.from(foundElements).forEach( el => results.push(el));
}
}
return results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment