Skip to content

Instantly share code, notes, and snippets.

@chrishalebarnes
Created September 7, 2017 19:13
Show Gist options
  • Save chrishalebarnes/ace844a0ea2ecc7dd3fdbdb55bd11588 to your computer and use it in GitHub Desktop.
Save chrishalebarnes/ace844a0ea2ecc7dd3fdbdb55bd11588 to your computer and use it in GitHub Desktop.
Safer QuerySelector
function qs(selector, callback, context) {
if(callback === undefined) return;
context = context || document;
const result = context.querySelector(selector);
if(result) {
return callback(result);
}
}
qs('.some .selector', (safeElement) => {
// operate on safeElement
// callback will not be called if `querySelector` returns null
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment