Skip to content

Instantly share code, notes, and snippets.

@electerious
Last active January 24, 2017 09:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save electerious/b49d7c295aac629779b2 to your computer and use it in GitHub Desktop.
Save electerious/b49d7c295aac629779b2 to your computer and use it in GitHub Desktop.
Select multiple elements and receive an array
const select = (query, elem = null) => {
elem = (elem==null ? document : elem)
let elems = elem.querySelectorAll(query)
if (elems==null) return []
else return Array.prototype.slice.call(elems, 0)
}
@electerious
Copy link
Author

Example:

select('a') // [<a>, …]
select('img') // [<img>, …]
select('img', select('p')) // [<img>, …]

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