Last active
December 29, 2021 15:37
-
-
Save vneri/9c72a3a669c60d58181d1f01ecf972e7 to your computer and use it in GitHub Desktop.
JavaScript querySelectorAll for all children and shadowRoot elements
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
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