Write function to get Elements By Attribute

Question: Write function to get the Elements By Attribute.

Answer:

function getElementsByAttribute(attribute){

var allElements = document.getElementsByTagName('*'),
elm,
found=[];

for (var i = 0; i < allElements.length; i++) {

elm = allElements[i];

if (elm.getAttribute(attribute)) {

found.push(elm);

}

}

return found;

}

Try it yourself: