Skip to content

Instantly share code, notes, and snippets.

@coding46
Created August 23, 2014 22:14
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 coding46/35b23bdfa82c5780c595 to your computer and use it in GitHub Desktop.
Save coding46/35b23bdfa82c5780c595 to your computer and use it in GitHub Desktop.
getElementsByAttribute
/**
* code:
* var elementsWithDataFoo = getElementsByAttribute( 'data-foo' );
* @param attribute
* @returns {Array}
*/
var getElementsByAttribute = function( attribute ) {
var resultSet = [],
allElements = document.getElementsByTagName( '*' );
for ( var i = 0, element; element = allElements[ i ]; i++ ) {
if ( element.getAttribute( attribute ) ) {
resultSet.push( element );
}
}
return resultSet;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment