Created
October 19, 2014 19:09
-
-
Save autonome/179afc6f9515a5008cb1 to your computer and use it in GitHub Desktop.
Query Selector Helpers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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