Section 25.94. Element.getAttribute( ): return the string value of a named attribute


25.94. Element.getAttribute( ): return the string value of a named attribute

DOM Level 1 Core:

25.94.1. Synopsis

 String getAttribute(String name); 

25.94.1.1. Arguments

name

The name of the attribute whose value is to be returned.

25.94.1.2. Returns

The value of the named attribute as a string. If the attribute is not defined, this method is supposed to return an empty string. Some implementations return null in this case, however.

25.94.2. Description

getAttribute( ) returns the value of a named attribute of an element. Note that the HTMLElement object defines JavaScript properties that match each of the standard HTML attributes, so you need to use this method with HTML documents only if you are querying the value of nonstandard attributes.

In XML documents, attribute values are not available directly as element properties and must be looked up by calling this method. For XML documents that use namespaces, use getAttributeNS( ).

25.94.3. Example

The following code illustrates two different ways of obtaining an attribute value for an HTML <img> element:

 // Get all images in the document var images = document.body.getElementsByTagName("img"); // Get the src attribute of the first one var src0 = images[0].getAttribute("src"); // Get the src attribute of the second simply by reading the property var src1 = images[1].src; 

25.94.4. See Also

Element.getAttributeNode( ), Element.getAttributeNS( ), Node




JavaScript. The Definitive Guide
JavaScript: The Definitive Guide
ISBN: 0596101996
EAN: 2147483647
Year: 2004
Pages: 767

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net