Skip to content

Instantly share code, notes, and snippets.

@kudinovfedor
Last active May 18, 2020 06:19
Show Gist options
  • Save kudinovfedor/de49156a0cee8102d7e9b4dd1ca7df25 to your computer and use it in GitHub Desktop.
Save kudinovfedor/de49156a0cee8102d7e9b4dd1ca7df25 to your computer and use it in GitHub Desktop.
Returns all element descendants of node that match selectors
/**
* 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