Ever run into a situation where you want to get an array of all elements with a specific attribute? Or even want elements with a certain value for that chosen attribute as well? Just add this code snippet to your script and your problem will be solved.
<!– Paste this snippet into your existing code –>
/* This script and many more are available free online at
The JavaScript Source :: http://javascriptsource.com
Copyright Robert Nyman, http://www.robertnyman.com
Free to use if this text is included */
function getElementsByAttribute(oElm, strTagName, strAttributeName, strAttributeValue){
var arrElements = (strTagName == “*” && document.all)? document.all : oElm.getElementsByTagName(strTagName);
var arrReturnElements = new Array();
var oAttributeValue = (typeof strAttributeValue != “undefined”)? new RegExp(“(^|\s)” + strAttributeValue + “(\s|$)”) : null;
var oCurrent;
var oAttribute;
for(var i=0; i<arrElements.length; i++){
oCurrent = arrElements[i];
oAttribute = oCurrent.getAttribute(strAttributeName);
if(typeof oAttribute == “string” && oAttribute.length > 0){
if(typeof strAttributeValue == “undefined” || (oAttributeValue && oAttributeValue.test(oAttribute))){
arrReturnElements.push(oCurrent);
}
}
}
return arrReturnElements;
}