Why I never wrote this function before is a mystery.
In my experience, people* designing XML response docs for Ajax-y type applications often re-use xHMTL attributes. href and src are common examples. One other one that I’ve run into pretty often is id. Since Javascript’s getElementById() relies on the document’s DTD defining the id attribute doing something like
xml_document.getElementById('myid')
returns null when pointed at the sort of informal XML documents people often spit out for these kind of things.
To remedy that, I wrote this:
function getElementByIdMXL(the_node,the_id) {
//get all the tags in the doc
node_tags = the_node.getElementsByTagName('*');
for (i=0;i<node_tags.length;i++) {
//is there an id attribute?
if (node_tags[i].hasAttribute('id')) {
//if there is, test its value
if (node_tags[i].getAttribute('id') == the_id) {
//and return it if it matches
return node_tags[i];
}
}
}
}
where the_node is the DTD-less XML document and the_id is the id to search for. It’s a pretty simple script really, but it would have been useful a couple of times in recent memory.
Looking at that bit of code made me want to create a version that searched for arbitrary attribute/value pairs. Since I’ve looked at getElementsByClass a thousand times it practically wrote itself:
function getElementsByAttribute(the_attribute, the_value, the_node) {
if ( the_node == null )
the_node = document;
var node_tags = the_node.getElementsByTagName('*');
var results = new Array();
for (i=0, j=0; i<node_tags.length;i++) {
if (node_tags[i].hasAttribute(the_attribute)) {
if (node_tags[i].getAttribute(the_attribute) == the_value) {
results[j] = node_tags[i];
j++;
}
}
}
return results;
}
The only real difference between the two is the second returns an array and the first just returns the one object.
These aren’t really groundbreaking, of course. They’re just a couple of bits of code I thought I’d share…
*that definitely includes me