PDA

View Full Version : Is there a function to return all attribute pairs?


deadseasquirrel
05-24-2004, 03:29 PM
From what I can tell there is no function to get all attributes of a tag, but I may be wrong, can somebody confirm this. I am still a newbie, so I am not sure if my ignorance is due to lack of knowledge or if my search has been thorough enough.

If there is no getAllAttribute() function, is there a hack to get to it? Or is there some underlying theory why that function should not exist, like "if you don't know what you're looking for, you shouldn't get it."

liorean
05-24-2004, 03:53 PM
you have [object Element].attributes, which is a collection of all attributes on an element. Iew incorrectly adds all possible attributes to it, while other browsers add only the set attributes to it. You can create an object containing all set attributes by doing the following, if you wish:var
oAttributes={},
i=0,
oAttr;
for((oAttr=elm.attributes.item(i++)))
if(oAttr.specified)
oAttributes[oAttr.name]=oAttr.value;

Vladdy
05-24-2004, 03:56 PM
DOM way:
Node.prototype.getAllAttributes = function()
{ rv = {};
for(var i=0; i<this.attributes.length; i++)
rv[this.attributes.item(i).nodeName]=this.attributes.item(i).nodeValue ;
return rv;
}
IE way:
function getAllAttributes(node)
{rv = {};
for(var i=0; i<node.attributes.length; i++)
if(node.attributes.item(i).specified)
rv[node.attributes.item(i).nodeName]=node.attributes.item(i).nodeValue
return rv;
}

EDIT: got beaten to the answer. Note to self: "take typing class" :eek:

swmr
05-25-2004, 02:13 AM
for((oAttr=elm.attributes.item(i++)))

cool :cool: