Entry
How can I get all the attributes supported by an object?
Can I know which attribute or event is supported by an element?
Can I find out which events are supported in which element?
Aug 30th, 2000 07:33
Martin Honnen, Rey Nuņez,
The DOM specification includes the attributes interface, which returns
a named-nodes map, a collection containing supported
attributes, even event attributes, of any given element. This is
implemented in NN6 and IE5+ where the results differ. While NN6 is only
returning some HMTL attributes but many event handlers are not returned
IE5+ seems to return a list of all possible attributes for all elements
so you get event handlers which might not be supported by the element
you query.
Typical syntax is
nodesMapRef = object.attributes
The following example shows how the attributes collection is used to
iterate through all the supported attributes of a specified object, in
this case the document BODY. The function returns a list of the
attribute names, whether the attribute was explicitly specified or not
in HTML or in script.
<script language="JavaScript">
<!--
function getAttribs(){
var atts, attribs = document.body.attributes;
atts = 'The BODY element has the following attributes:\n';
for (a=0; a<attribs.length; a++)
atts += '\n'+attribs[a].nodeName;
return (atts)
}
//-->
</script>
<a href="javascript:getAttribs()">Get Attributes</a>
Note that the collection does not include custom attributes assigned in
script. Also, the collection returns null if the specified object is
not an element.