How can you do the equivalent to
document.querySelectorAll(\'[data-foo]\')
where query
A little modification on @kevinfahy 's answer, to allow getting the attribute by value if needed:
function getElementsByAttributeValue(attribute, value){
var matchingElements = [];
var allElements = document.getElementsByTagName('*');
for (var i = 0, n = allElements.length; i < n; i++) {
if (allElements[i].getAttribute(attribute) !== null) {
if (!value || allElements[i].getAttribute(attribute) == value)
matchingElements.push(allElements[i]);
}
}
return matchingElements;
}