Last active
May 18, 2020 06:19
-
-
Save kudinovfedor/de49156a0cee8102d7e9b4dd1ca7df25 to your computer and use it in GitHub Desktop.
Returns all element descendants of node that match selectors
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
/** | |
* Selector All | |
* | |
* @description | |
* Returns all element descendants of node that match selectors. | |
* | |
* @example | |
* selectorAll('div'); | |
* selectorAll('div', document); | |
* | |
* @param {string} elements - DOM elements | |
* @param {Element} [context=null] | |
* @returns {Array<Element>} | |
*/ | |
const selectorAll = (elements, context = null) => { | |
let items; | |
if (context instanceof Element) { | |
items = context.querySelectorAll(elements); | |
} else { | |
items = document.querySelectorAll(elements); | |
} | |
let index = 0, length = items.length, itemsArray = []; | |
for (; index < length; index++) { | |
itemsArray.push(items[index]); | |
} | |
return itemsArray; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment