Name
XML.attributes Property — an object whose properties store element attributes
Availability
Flash 5
Synopsis
theNode.attributes.attributeIdentifier theNode.attributes[attributeNameInQuotes]
Access
Read/write
Description
The attributes
property stores the names and
values of attributes defined by theNode
.
For example, the ALIGN
attribute of this
P
tag:
<P ALIGN="CENTER">this is a paragraph</P>
is accessed using
theNode.
attributes.ALIGN
or
theNode.
attributes["ALIGN"]
.
If the P
tag were the only tag in our XML source,
we could access the ALIGN
attribute as follows:
// Create an XML object hierarchy myDoc = new XML('<P ALIGN="CENTER">this is a paragraph</P>'); // Access the ALIGN attribute. Displays: "CENTER" trace(myDoc.firstChild.attributes.ALIGN); // Set the ALIGN attribute myDoc.firstChild.attributes.ALIGN = "LEFT";
The attributes
property is itself an object. We
can add new properties to the attributes
object,
thereby adding new attributes to theNode
,
as follows:
// Add a CLASS attribute to the P tag myDoc.firstChild.attributes.CLASS = "INTRO"; // firstChild now represents the XML source: // <P ALIGN="CENTER" CLASS="INTRO">this is a paragraph</P>
Because attributes
is not an array, it
doesn’t contain a length
property. Instead,
we can access all the attributes defined on an element using a
for-in
loop:
var count = 0; for(var prop in theNode.attributes) { trace("attribute " + prop + " has the value " + theNode.attributes[prop]); count++; } trace ("The node has " + count + " attributes."); ...
Get ActionScript: The Definitive Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.