Created
September 7, 2017 19:13
-
-
Save chrishalebarnes/ace844a0ea2ecc7dd3fdbdb55bd11588 to your computer and use it in GitHub Desktop.
Safer QuerySelector
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
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