Skip to content

Instantly share code, notes, and snippets.

@Loac-fr
Created April 20, 2015 09:32
Show Gist options
  • Save Loac-fr/751082417a999c9893ea to your computer and use it in GitHub Desktop.
Save Loac-fr/751082417a999c9893ea to your computer and use it in GitHub Desktop.
Lea Verou's add class without jQuery
// http://youmightnotneedjquery.com/
// 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));
}
// example use :
$$("article").forEach(function(article){
article.classList.add("read");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment