Skip to content

Instantly share code, notes, and snippets.

@piyonishi
Created June 3, 2015 15:24
Show Gist options
  • Save piyonishi/570171dc63570dbd4915 to your computer and use it in GitHub Desktop.
Save piyonishi/570171dc63570dbd4915 to your computer and use it in GitHub Desktop.
// Cache the querySelector/All for easier and faster reuse
window.$ = document.querySelectorAll.bind(document);
window.$$ = document.querySelector.bind(document);
// Get element(s) by CSS selector:
window.qs = function (selector, scope) {
return (scope || document).querySelector(selector);
};
window.qsa = function (selector, scope) {
return (scope || document).querySelectorAll(selector);
};
// Returns first element that matches CSS selector {expr}.
// Querying can optionally be restricted to {container}’s descendants
function $(expr, container) {
return typeof expr === "string"? (container || document).querySelector(expr) : expr || null;
}
// Returns all elements that match CSS selector {expr} as an array.
// Querying can optionally be restricted to {container}’s descendants
function $$(expr, container) {
return [].slice.call((container || document).querySelectorAll(expr));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment