Created
April 7, 2019 19:36
-
-
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.
This file contains hidden or 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 | |
* | |
* @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