Skip to content

Instantly share code, notes, and snippets.

@tarciozemel
Created April 20, 2015 17:11
Show Gist options
  • Save tarciozemel/0bf35374a64fe6c9e2d1 to your computer and use it in GitHub Desktop.
Save tarciozemel/0bf35374a64fe6c9e2d1 to your computer and use it in GitHub Desktop.
JavaScript: lightweight selector implementation
//
// 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