Returns a collection of objects with attribute values matching attrValue. An optional second argument allows you to query only specific attributes.
attrValue
attrName
nObjects = so_getElementsByAttributeValue("toc","class");
To get a collection of all objects with a class of "toc": so_getElementsByAttributeValue("toc","class");
To get a collection of all objects with any attribute with a value of "foo": so_getElementsByAttributeValue("foo");
function so_getElementsByAttributeValue(attrValue) {
var all = document.getElementsByTagName("*");
var elem = new Array()
var i=0;
while(all[i]) {
if(arguments[1]) {
attrName = arguments[1];
if(all[i].hasAttribute) {
if(all[i].hasAttribute(attrName)) if(all[i].getAttribute(attrName) == attrValue) elem[elem.length] = all[i];
} else {
if(all[i].tagName != "!") {
for(j=0;j<all[i].attributes.length;j++) {
attr = all[i].attributes[j];
if(attr.value) if(attr.name.toLowerCase() == attrName.toLowerCase() && attr.value == attrValue) elem[elem.length] = all[i];
}
}
}
} else {
for(j=0;j<all[i].attributes.length;j++) if(all[i].tagName != "!") if(all[i].attributes[j].value == attrValue) elem[elem.length] = all[i];
}
i++;
}
return elem;
}
This code is issued under a Creative Commons license. You do not need my permission to use it, though I'm always interested in seeing where my code has ended up if you want to drop me a line.
slayeroffice main