Skip to content

Instantly share code, notes, and snippets.

@autonome
Created October 19, 2014 19:09
Show Gist options
  • Save autonome/179afc6f9515a5008cb1 to your computer and use it in GitHub Desktop.
Save autonome/179afc6f9515a5008cb1 to your computer and use it in GitHub Desktop.
Query Selector Helpers
/*
Quick and easy conversion of nodelist to array.
You're on your own for error handling - invalid selectors throw an exception!
Example:
qsa['.fooble'].forEach(function(node) {
// do stuff
});
*/
function qsa(str) {
return [].slice.call(document.querySelectorAll(str));
}
/*
The Promise version.
Example:
qsap('.fooble').then(function(nodes) {
nodes.forEach(function(node) {
// do stuff
});
}, function(err){
console.log('bad selector!');
})
*/
function qsap(str) {
return new Promise(function(resolve, reject) {
try {
resolve([].slice.call(document.querySelectorAll(str)));
}
catch(ex) {
reject(ex);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment