Skip to content

Instantly share code, notes, and snippets.

@jonasporto
Created July 30, 2014 19:29
Show Gist options
  • Select an option

  • Save jonasporto/0104d736019a5193c36b to your computer and use it in GitHub Desktop.

Select an option

Save jonasporto/0104d736019a5193c36b to your computer and use it in GitHub Desktop.
get elements by atributes javascript
function getAllElementsWithAttribute(attribute)
{
var matchingElements = [];
var allElements = document.getElementsByTagName('*');
for (var i = 0, n = allElements.length; i < n; i++)
{
if (allElements[i].getAttribute(attribute))
{
// Element exists with attribute. Add to array.
matchingElements.push(allElements[i]);
}
}
return matchingElements;
}
//by http://stackoverflow.com/questions/9496427/can-i-get-elements-by-attribute-selector-when-queryselectorall-is-not-available
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment