Created
July 30, 2016 10:39
-
-
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
This file contains 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
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