Skip to content

Instantly share code, notes, and snippets.

@kudinovfedor
Created April 7, 2019 19:36
Show Gist options
  • Save kudinovfedor/988a5ad7474ed86b5453044f00632113 to your computer and use it in GitHub Desktop.
Save kudinovfedor/988a5ad7474ed86b5453044f00632113 to your computer and use it in GitHub Desktop.
Returns the first element that is a descendant of node that matches selectors.
/**
* Selector
*
* @description
* Returns the first element that is a descendant of node that matches selectors.
*
* @example
* selector('div');
* selector('div', document);
*
* @param {string} element - DOM element
* @param {Element} [context=null]
* @returns {*}
*/
const selector = (element, context = null) => {
if (context instanceof Element) {
return context.querySelector(element);
}
return document.querySelector(element);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment