Skip to content

Instantly share code, notes, and snippets.

@andirady
Last active April 7, 2018 07:22
Show Gist options
  • Save andirady/87979e7b9e8fb2ee49df83287c6536b8 to your computer and use it in GitHub Desktop.
Save andirady/87979e7b9e8fb2ee49df83287c6536b8 to your computer and use it in GitHub Desktop.
Aliases for JS methods
function $(a,b) {
if (b === undefined)
return document.querySelector(a);
return a.querySelector(b);
}
function $$(a,b) {
if (b === undefined)
return document.querySelectorAll(a);
return a.querySelectorAll(b);
}
@andirady
Copy link
Author

andirady commented Apr 7, 2018

Aliases for querySelector and querySelectorAll. Example usage:

$$($('#links'), 'a').forEach(a => a.addEventListener('click', e => alert("Hello")))
// equivalent to
document.querySelector('#links').querySelectorAll('a').forEach(a => a.addEventListener('click', e => alert("Hello")))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment