Skip to content

Instantly share code, notes, and snippets.

@jsmrcaga
Created July 30, 2016 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsmrcaga/b3c14be5138256dca1d94993d5e6d7b4 to your computer and use it in GitHub Desktop.
Save jsmrcaga/b3c14be5138256dca1d94993d5e6d7b4 to your computer and use it in GitHub Desktop.
Function to get all elements that have an attribute that starts with a given string
function getElementsByAttributeName(startsWith, type){
return Array.from(document.querySelectorAll(type || '*')).filter(function(el){
var ok = false;
Array.from(el.attributes).forEach( function(attr){
if(startsWith instanceof RegExp){
ok = attr.name.match(startsWith);
} else {
ok = attr.name.startsWith(startsWith);
}
});
return ok;
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment