Section 25.106. Element.setAttribute( ): create or change an attribute of an element


25.106. Element.setAttribute( ): create or change an attribute of an element

DOM Level 1 Core:

25.106.1. Synopsis

 void setAttribute(String name,                   String value)     throws DOMException; 

25.106.1.1. Arguments

name

The name of the attribute to be created or modified.


value

The string value of the attribute.

25.106.1.2. Throws

This method may throw a DOMException with the following code values:


INVALID_CHARACTER_ERR

The name argument contains a character that is not allowed in HTML or XML attribute names.


NO_MODIFICATION_ALLOWED_ERR

This element is read-only and does not allow modifications to its attributes.

25.106.2. Description

This method sets the specified attribute to the specified value. If no attribute by that name already exists, a new one is created.

Note that HTMLElement objects of an HTML document define JavaScript properties that correspond to all standard HTML attributes. Thus, you need to use this method only if you want to set a nonstandard attribute.

25.106.3. Example

 // Set the TARGET attribute of all links in a document var links = document.body.getElementsByTagName("a"); for(var i = 0; i < links.length; i++) {     links[i].setAttribute("target", "newwindow");     // Or more easily: links[i].target = "newwindow" } 

25.106.4. See Also

Element.getAttribute( ), Element.removeAttribute( ), Element.setAttributeNode( )




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