# Element : Node
Return to: HTML DOM Level 2 index

Represents an element in an HTML or XML document.

## Constructors

Constructor | Action | IE | Mozilla | Netscape | Opera | Safari
--- | --- | --- | --- | --- | --- | ---
Element Constructor() : Element Represents an element in an HTML or XML document. | Show Details

### Element() : Element

Represents an element in an HTML or XML document.

Returns

Element |

Visibility
  internal

## Inherited Properties

Property | Action | IE | Mozilla | Netscape | Opera | Safari
--- | --- | --- | --- | --- | --- | ---
attributes : NamedNodeMap Attributes for an element node. | Show Details | 4.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+

  For examples, see the quirksmode test page:

  http://www.quirksmode.org/dom/tests/attributes.html

Remarks

  This property returns a listing of all the attributes bound to this `Node`. Since only `Element` nodes support attributes, most `Node` types will return a null value for this property.

  `NamedNodeMap`s are essentially the DOM-equivilant of a hash. This property provides an alternative to the specific <method>Element.getAttribute</method> and <method>Element.hasAttribute</method> methods, which are usually used when you already know which attributes an element has. For those circumstances when you want to do some automatic discovery of attributes and their values, this property provides a convenient way to iterate through the element's attributes.

  This property also has the added benefit of being usable across all nodes, not just `Element` nodes. Therefore no checking needs to be done ahead of time to verify if a node is an `Element`; rather you can iterate over the <property>attributes</property> list, since any element that has no attributes, or any other DOM node, will have a null value for this property.

Availability

  HTML DOM Level 1|HTML DOM Level 2|W3C

childNodes : NodeList Child nodes of the current node. | Show Details | 4.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+

  For examples, see the quirksmode test page:

  http://www.quirksmode.org/dom/tests/children.html

Remarks

  The list of nodes that are immediate children of this node. The DOM is a heirarchial structure of `Node` objects. This property, combined with the <property>parentNode</property> properties provide the basics for defining this structure. Several convenience properties are provided for accessing some frequently-used children nodes, namely the <property>firstChild</property> and <property>lastChild</property> properties.

  Since the `NodeList` class can be referenced as an array as well as its object-oriented interface, one can easily iterate through the contents of a node using standard JavaScript `for` loops. Because this property is read-only, you cannot alter the `NodeList` to effect this node's contents. Instead, the methods for addeding and removing nodes should be used, which will result in this being updated automatically.

Availability

  HTML DOM Level 1|HTML DOM Level 2|W3C

firstChild : Node First child node of the current node. | Show Details | 4.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+

  For examples, see the quirksmode test page:

  http://www.quirksmode.org/dom/tests/nodetree.html

Remarks

  Returns the first child node from the list of the current node's children. If this node doesn't have any children, a null value will be returned.

  This is functionally equivilant to invoking `node.<property>childNodes</property>.<method>NodeList.item</method>(0)`, though this method is much more suscinct and doesn't require any error trapping if the child node doesn't exist.

Availability

  HTML DOM Level 1|HTML DOM Level 2|W3C

lastChild : Node Last child node of the current node. | Show Details | 4.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+

  For examples, see the quirksmode test page:

  http://www.quirksmode.org/dom/tests/nodetree.html

Remarks

  Returns the last child node from the list of the current node's children. If this node doesn't have any children, a null value will be returned.

  This is functionally equivilant to invoking `node.<property>childNodes</property>.<method>NodeList.item</method>(node.<property>childNodes</property>.<property>NodeList.length</property> - 1)`, though this method is much more suscinct and doesn't require any error trapping if the child node doesn't exist.

Availability

  HTML DOM Level 1|HTML DOM Level 2|W3C

localName : String Local part of an element or attribute name if it the node was defined with an XML Namespace. | Show Details | 4.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+

Availability

  HTML DOM Level 2|W3C

namespaceURI : String URI of the namespace for an element or attribute node if the node was defined with an XML Namespace. | Show Details | 4.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+

Availability

  HTML DOM Level 2|W3C

nextSibling : Node Sibling node immediately after the current node. | Show Details | 4.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+

  For examples, see the quirksmode test page:

  http://www.quirksmode.org/dom/tests/nodetree.html

Remarks

  Returns the next node to the current one within the parent node's children. If this node is the last child within it's parent, then a null value will be returned.

  When working with a node, it is often useful to be able to manipulate or search the nodes surrounding it. For instance, if given a node representing a label, you might want to find the text node immediately next to it. To help with this, the <property>nextSibling</property> property allows you to traverse to neighboring nodes in the DOM.

Availability

  HTML DOM Level 1|HTML DOM Level 2|W3C

nodeName : String Name of the node. Same as tag name for element nodes. | Show Details | 4.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+

  For examples, see the quirksmode test page:

  http://www.quirksmode.org/dom/tests/aboutnodes.html

Remarks

  This returns a name that represents this node's name. Depending on the type of object this is, this property will either return the name identifying this object; for instance, if this node is a `Element` or `Attr` object, then the tag or attribute name is returned. In other cases some place-holder text will be returned.

  <property>nodeName</property> node-type values<columns><column><name>Type</name><align>left</align></column><column><name>Value</name><align>left</align></column></columns><rows><row><column>`Attr`</column><column> Attribute name </column></row><row><column>`CDATASection`</column><column>`#cdata-section`</column></row><row><column>`Comment`</column><column>`#comment`</column></row><row><column>`Document`</column><column>`#document`</column></row><row><column>`DocumentFragment`</column><column>`#document-fragment`</column></row><row><column>`DocumentType`</column><column> Document type name </column></row><row><column>`Element`</column><column> Tag name </column></row><row><column>`Entity`</column><column> Entity name </column></row><row><column>`EntityReference`</column><column> Name of entity referenced </column></row><row><column>`Notation`</column><column> Notation name </column></row><row><column>`ProcessingInstruction`</column><column> Target </column></row><row><column>`Text`</column><column>`#text`</column></row></rows>

  If <glossary>XML Namespaces</glossary> were defined for this node and this node is a `Element` or `Attr` object (since these are the only node types that support the use of <glossary>namespaces</glossary>), this property returns the node name including the <property>prefix</property> and <property>localName</property>.

  Returns an upper case tag name.

Availability

  HTML DOM Level 1|HTML DOM Level 2|W3C

nodeType : Number Type of node. See Remarks for valid values. | Show Details | 5.5+ | 1.0+ | 6.0+ | 7.0+ | 1.0+

- IE: IE 5.0 on Windows does not assign nodeType attributes. Fixed in IE 5.5.

  For examples, see the quirksmode test page:

  http://www.quirksmode.org/dom/tests/aboutnodes.html

Remarks

  This property returns an integer indicating what type of DOM `Node` this object represents.

  Because the `Node` class is inherited by several other, more specific DOM objects, this property is a programmatic way of determining what an arbitrary node is, and therefore can tell the programmer how to interact with it. There are a series of constants defined in this object that can be used to refer to this property:

  <property>nodeList</property> constant values<columns><column><name>Value</name><align>right</align></column><column><name>Constant</name><align>left</align></column></columns><rows><row><column>`1`</column><column><property>ELEMENT_NODE</property></column></row><row><column>`2`</column><column><property>ATTRIBUTE_NODE</property></column></row><row><column>`3`</column><column><property>TEXT_NODE</property></column></row><row><column>`4`</column><column><property>CDATA_SECTION_NODE</property></column></row><row><column>`5`</column><column><property>ENTITY_REFERENCE_NODE</property></column></row><row><column>`6`</column><column><property>ENTITY_NODE</property></column></row><row><column>`7`</column><column><property>PROCESSING_INSTRUCTION_NODE</property></column></row><row><column>`8`</column><column><property>COMMENT_NODE</property></column></row><row><column>`9`</column><column><property>DOCUMENT_NODE</property></column></row><row><column>`10`</column><column><property>DOCUMENT_TYPE_NODE</property></column></row><row><column>`11`</column><column><property>DOCUMENT_FRAGMENT_NODE</property></column></row><row><column>`12`</column><column><property>NOTATION_NODE</property></column></row></rows>

Availability

  HTML DOM Level 1|HTML DOM Level 2|W3C

nodeValue : String Value of the current node. | Show Details | 4.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+

  For examples, see the quirksmode test page:

  http://www.quirksmode.org/dom/tests/aboutnodes.html

Remarks

  This represents the value of the node. Only `Attr`, `CDATASection`, `Comment`, `ProcessingInstruction`, and `Text` objects can contain a value in this property. For all other types of objects this property will return null, and setting it to a different value has no effect.

Availability

  HTML DOM Level 1|HTML DOM Level 2|W3C

ownerDocument : Document Document object that contains this node. | Show Details | 6.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+

  For examples, see the quirksmode test page:

  http://www.quirksmode.org/dom/tests/document.html

Remarks

  Refers to the `Document` object that this node exists in. Because a node must be created as a child of a `Document`, and since `Node` objects cannot be moved arbitrarily from one document to another (not without duplicating it), this is a way of referring to the parent document for a node so that document-wide method calls can be used for a node.

Availability

  HTML DOM Level 1|HTML DOM Level 2|W3C

parentNode : Node Parent node of the current node. | Show Details | 4.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+

  For examples, see the quirksmode test page:

  http://www.quirksmode.org/dom/tests/nodetree.html

Remarks

  This property contains a reference to the parent node for the current node. Since the DOM is a heirarchial structure of nodes, the <property>parentNode</property> and <property>childNodes</property> properties tie the collection of nodes together.

  Not all nodes can have parents however. `Attr`, `Document`, `DocumentFragment`, `Entity` and `Notation` objects and as such will contain a null value for this property. As well this property will be null for newly-created nodes that have yet to be added to a location within the DOM tree.

Availability

  HTML DOM Level 1|HTML DOM Level 2|W3C

prefix : String Namespace prefix for an element or attribute node if the node was defined with an XML Namespace. | Show Details | 4.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+

Availability

  HTML DOM Level 2|W3C

previousSibling : Node Sibling node immediately before the current node. | Show Details | 4.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+

  For examples, see the quirksmode test page:

  http://www.quirksmode.org/dom/tests/nodetree.html

Remarks
  Returns the previous node to the current one within the parent node's children. If this node is the first child within it's parent, then a null value will be returned.
Availability

  HTML DOM Level 1|HTML DOM Level 2|W3C

## Properties

Property | Action | IE | Mozilla | Netscape | Opera | Safari
--- | --- | --- | --- | --- | --- | ---
tagName : String The tag name for this element | Show Details | 5.0+ | 1.0+ | 6.0+ | 8.0+ | 1.3+

  For examples, see the quirksmode test page:

  http://www.quirksmode.org/dom/tests/aboutnodes.html

Remarks
  Identifies the name of an element in a source document, including any namespace prefix. For example, in the following XML content, `tagName` value of the outer element (the `` element) returns `"example1"`, while the inner element has a `tagName` value of `"test:example2"`, even though the element name itself, without the `namespace prefix` is simply `"example2"`.:

Availability

  HTML DOM Level 2|W3C

## Inherited Functions

Method | Action | IE | Mozilla | Netscape | Opera | Safari
--- | --- | --- | --- | --- | --- | ---
hasAttributes() : Boolean Returns true if the node is an element node with attributes. | Show Details | no | 1.0+ | 6.0+ | 7.0+ | 1.3+

Returns

Boolean |

  ### Using hasAttributes

      var obj = document.Document.getElementById('EmailAddress');
                              if (obj.hasAttributes()) {
                              window.alert("The node " + obj.Node.nodeName + " has attributes");
                              }

  For more examples, see the quirksmode test page:

  http://www.quirksmode.org/dom/tests/attributes3.html

Remarks
  Returns true if this node is an `Element` and has any attributes. Returns false if there are none. Equivilant to checking `node.`attributes`.<property>NamedNodeMap.length</property> > 0`.
See Also

  Element.getAttribute|Element.hasAttribute|Node.attributes

Availability

  HTML DOM Level 1|HTML DOM Level 2|W3C

hasChildNodes() : Boolean Returns true if the node has child nodes. | Show Details | 4.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+

Returns

Boolean |

  ### Using hasChildNodes

      var obj = document.Document.getElementById('EmailAddress');
                              if (obj.hasChildNodes()) {
                              window.alert("The node " + obj.Node.nodeName + " has children");
                              }

Remarks
  Returns true if this node has any child nodes. Returns false if there are none. Equivilant to checking `node.`childNodes`.`NodeList.length` > 0`.
See Also

  Node.childNodes

Availability

  HTML DOM Level 1|HTML DOM Level 2|W3C

isSupported(String feature, [String version]) : Boolean Returns true if the specified feature and version are supported. | Show Details | no | 1.0+ | 6.0+ | 7.0+ | 1.0+

Parameters

String | feature | Name of the feature.
String | version | (optional)Version of the feature.

Returns

Boolean |

Remarks
  Tests whether a node supports the specified feature. Similar to the `DOMImplementation.hasFeature` method and takes the same feature names. If version is null, returns true if any version of the feature is supported.
See Also

  DOMImplementation.hasFeature

Availability

  HTML DOM Level 2|W3C

normalize() : void Merges text nodes adjacent to the element to create a normalized DOM. | Show Details | N/A | 1.0+ | 6.0+ | 7.0+ | 1.0+

- IE: May crash IE 6.0.

Returns

void |

  For examples, see the quirksmode test page:

  http://www.quirksmode.org/dom/tests/splittext.html

Remarks
  Normalizes any `Text` nodes contained under this node and all of its children. Merges any adjacent `Text` nodes into one object, so that the nodes reflect how they would be structured if this XML document were freshly loaded.

  `Normalize()` is useful for manipulating the text content of nodes for editing. Instead of worrying about altering existing `Text` nodes, you can simply add new nodes and perform the `normalize` when you finish your updates.

Availability

  HTML DOM Level 1|HTML DOM Level 2|W3C

## Functions

Method | Action | IE | Mozilla | Netscape | Opera | Safari
--- | --- | --- | --- | --- | --- | ---
static getAttribute(String name) : String Gets an attribute value by name. | Show Details | 5.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+

- IE: Incorrectly returns the value "object" on the "style" attribute.
- Opera: Returns internal style information on the "style" attribute.

Parameters

String | name | The name of the attribute to get the value for.

Returns

String |

  ### Using getAttribute

      var node = document.getElementById('toolbar');
                              if (node.getAttribute('visible') == true) {
                              // Magic happens here
                              }

  For more examples, see the quirksmode test page:

  http://www.quirksmode.org/dom/tests/attributes2.html

Remarks

  This method retrieves one of the element's attributes that has the supplied `name`, and returns its `value`. If no attribute by that name exists as a child of the given element, an empty string is returned.

  Its important to note that a copy of the attribute's value is returned, rather than a reference to the actual value. This means that you cannot modify the attribute value using this method. You need to use another method (like `getAttributeNode` or `getAttributeNodeNS`) that returns the `Attr` object itself to make changes.

See Also

  Element.getAttributeNode|Node.attributes

Availability

  HTML DOM Level 2|W3C

static getAttributeNode(String name) : Attr Gets an attribute node by name | Show Details | 6.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+

Parameters

String | name | The name of the attribute to get the Attr node object for.

Returns

Attr |

  ### Using getAttributeNode

      var attr = element.getAttributeNode("class");
                              if (! attr.specified) {
                              // This attribute is a default value
                              }

  For more examples, see the quirksmode test page:

  http://www.quirksmode.org/dom/tests/attributes2.html

Remarks
  This method retrieves an attribute from an element, as an `Attr` object, rather than as a `String`. This is similar to the `getAttribute` method, except that method only returns the attribute's value. `getAttributeNode` lets you access a given attribute object without having to iterate over the `attributes` property.
See Also

  Element.getAttribute|Element.getAttributeNodeNS|Node.attributes

Availability

  HTML DOM Level 2|W3C

static getAttributeNodeNS(String namespaceURI, String localName) : Attr Gets an attribute node by local name and namespace URI. | Show Details | no | 1.0+ | 6.0+ | 8.0+ | no

Parameters

String | namespaceURI | The namespace URI of the attribute node to get.
String | localName | Local name of the attribute node to get.

Returns

Attr |

  ### Using getAttributeNodeNS

      var attr = element.getAttributeNodeNS("http://mozref.com/NS/test", "name");
                              if (! attr.specified) {
                              // This attribute is a default value
                              }

Remarks

  Similar to <method>getAttributeNode</method> and to <method>getAttributeNS</method>, this method retrieves an attribute's `Attr` object from the current `Element` by specifying both the attribute's <glossary>namespace URI</glossary> and <glossary>local name</glossary>.

  Supplying the <varname>namespaceURI</varname> parameter with a `null` value is functionally equivilant to calling <method>getAttributeNode</method>.

See Also

  Element.getAttributeNode|Element.getAttributeNS

Availability

  HTML DOM Level 2|W3C

static getAttributeNS(String namespaceURI, String localName) : String Gets an attribute value by local name and namespace URI | Show Details | no | 1.0+ | 6.0+ | 8.0+ | no

Parameters

String | namespaceURI | The namespace URI of the attribute to get.
String | localName | The local name of the attribute to get.

Returns

String |

  ### Using getAttributeNS

      /* Given the source XML:

                              */
                              var value = element.getAttributeNS("http://mozref.com/NS/test", "name");

Remarks

  Similar to <method>getAttribute</method>, this method retrieves one of the element's attributes. In this instance though, both the <glossary>namespace URI</glossary> and local name of the attribute can be used to select the appropriate attribute.

  Supplying the <varname>namespaceURI</varname> parameter with a `null` value is functionally equivilant to calling <method>getAttribute</method>.

See Also

  Element.getAttribute|Element.getAttributeNodeNS

Availability

  HTML DOM Level 2|W3C

static getElementsByTagName(String name) : NodeList Returns a NodeList array of all Element nodes descending from the specified Element. | Show Details | 5.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+

- IE: Does not work in IE 5.0 on Windows.

Parameters

String | name | Tag name of the element(s) to retrieve. (Use "*" as a wild card to return all tags.)

Returns

NodeList |

  ### Using getElementsByTagName

      var ctxt = document.getElementById("sidebar-div");
                              for ( var link in ctxt.getElementsByTagName("a") ) {
                              // Do something with this link
                              }

  For additional examples, see the quirksmode test page:

  http://www.quirksmode.org/dom/tests/basics.html

Remarks

  When trying to retrieve elements from the DOM, you do not often know the ID of the elements in question, but you know the type of element they are. Therefore, the `Document` object provides the getElementsByTagName method for searching the DOM and returning a list of elements matching the supplied tag name.

  However, there may be many instances of that same XML element that are not relevant. Therefore, this same method is made available on all `Element` nodes, permitting you to search from the potentially much smaller subset of elements that are descendents of the current element.

See Also

  Document.getElementById|Document.getElementsByTagName|HTMLDocument.getElementsByName

Availability

  HTML DOM Level 2|W3C

static getElementsByTagNameNS(String namespaceURI, String localName) : NodeList Gets a list of all descendent elements by local tag name and namespace URI | Show Details | no | 1.0+ | 6.0+ | 8.0+ | no

Parameters

String | namespaceURI | Namespace URI of the elements to get. (Use "*" as a wild card.)
String | localName | The local name of the elements to get. (Use "*" as a wild card to return all tag names.)

Returns

NodeList |

  ### Using getElementsByTagNameNS

      var ctxt = document.getElementById("toolbar-menubar");
                              var menuitems = ctxt.getElementsByTagNameNS(
                              "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul",
                              "menuitem");
                              for ( var menuitem in menuitems ) {
                              // Do something with this menu item
                              }

Remarks

  Similar to <method>getElementsByTagName, this method retrieves an element's descendent `Element` objects that match the supplied <glossary>namespace URI</glossary> and <glossary>local name</glossary>.

  Supplying the <varname>namespaceURI</varname> parameter with a `null` value is functionally equivilant to calling <method>getElementsByTagName.

See Also

  Document.getElementsByTagNameNS|Element.getElementsByTagName

Availability

  HTML DOM Level 2|W3C

hasAttribute(String name) : Boolean Determines if the given attribute exists on this element. | Show Details

Parameters

String | name | Name of the attribute to check.

Returns

Boolean |

  ### Using hasAttribute

      if (!element.hasAttribute("id")) {
                              element.setAttribute("id", "newvalue");
                              }

  For more examples, see the quirksmode test page:

  http://www.quirksmode.org/dom/tests/attributes3.html

Remarks
  Use this method to verify that an attribute exists on an Element before retrieving that attribute.
See Also

  Attr.specified|Element.getAttribute|Element.setAttribute

Availability

  HTML DOM Level 2HTML DOM Level 2|W3C

hasAttributeNS(String namespaceURI, String localName) : Boolean Determines if the given attribute local name and namespace URI exists on this element | Show Details | no | 1.0+ | 6.0+ | 8.0+ | 1.3+

Parameters

String | namespaceURI | Namespace URI of the attribute to check.
String | localName | Local name of the attribute to check.

Returns

Boolean |

  ### Using hasAttributeNS

      /* Given the source XML:

                              */
                              if (!element.hasAttributeNS("http://mozref.com/NS/test", "name")) {
                              element.setAttributeNS("http://mozref.com/NS/test", "name", "newvalue");
                              }

Remarks
  Similar to `hasAttribute`, this method returns a boolean value indicating an attribute exists on the given element. In this instance, the `hasAttributeNS` method takes an extra parameter specifying the `namespace URI` for the attribute, enabling you to check for attributes that have a namespace associated with them.
See Also

  Element.getAttributeNS|Element.setAttributeNS

Availability

  HTML DOM Level 2|W3C

removeAttribute(String name) : void Removes an attribute by name | Show Details | 4.0+ | 1.0+ | 6.0+ | 8.0+ | 1.0+

Parameters

String | name | Name of the attribute to remove.

Returns

void |

  ### Using removeAttribute

      if (element.hasAttribute("id")) {
                              element.removeAttribute("id");
                              }

  For more examples, see the quirksmode test page:

  http://www.quirksmode.org/dom/tests/attributes2.html

Remarks
  Removes the specified attribute from the element. If the attribute has a defined default value, it will be set to that default value.
Throws
    Raises a NO_MODIFICATION_ALLOWED_ERR error if the node is read-only.
See Also

  Element.getAttribute|Element.setAttribute|Node.attributes

Availability

  HTML DOM Level 2|W3C

static removeAttributeNode(Attr oldAttr) : Attr Removes the specified attribute node | Show Details | 6.0+ | 1.0+ | 6.0+ | 8.0+ | 1.0+

- IE: Minimal support in IE 6.0 (Windows) and IE 5 (Mac).

Parameters

Attr | oldAttr | Node to remove from the attribute.

Returns

Attr |

  For examples, see the quirksmode test page:

  http://www.quirksmode.org/dom/tests/attributes2.html

Remarks
  Removes an attribute from an element. If the attribute has a defined default value, the new attribute node value will immediately be set to that.
Throws
    Raises a NO_MODIFICATION_ALLOWED_ERR error if the node is read-only. Raises a NOT_FOUND_ERR error if oldAttr object is not an attribute of this element.
See Also

  Attr|Element.removeAttribute

Availability

  HTML DOM Level 2|W3C

removeAttributeNS() : void Removes an attribute by local name and namespace URI | Show Details | no | 1.0+ | 6.0+ | 8.0+ | no

Returns

void |

Remarks
  This method removes an attribute from an `Element` based on the attribute's <glossary>local name</glossary> and <glossary>namespace URI</glossary>. If this attribute has a default value, a new attribute with the default value will immediately replace it, with the same <glossary>local name</glossary>, <glossary>namespace URI</glossary> and <glossary>namespace prefix</glossary> as the original attribute.
Throws
    Raises a NO_MODIFICATION_ALLOWED_ERR error if the node is read-only.
See Also

  Element.getAttributeNS|Element.removeAttribute|Element.setAttributeNS

Availability

  HTML DOM Level 2|W3C

setAttribute(String name, String value) : void Adds a new attribute. | Show Details | 5.0+ | 1.0+ | 6.0+ | 8.0+ | 1.0+

- IE: IE removes event attributes when you try to set style attributes.

Parameters

String | name | Name of the attribute to set.
String | value | String value of the attribute to set.

Returns

void |

  ### Using setAttribute

      var node = document.getElementById('address-list');
                              document.setAttribute('ref', 'urn:root');
                              document.setAttribute('datasources', 'http://mozref.com/reference/objects.rdf');

  For more examples, see the quirksmode test page:

  http://www.quirksmode.org/dom/tests/attributes2.html

Remarks
  Sets either a new attribute value or updates an existing attribute value. This method can only set values as text strings, not HTML. Entities will not be parsed. To assign values containing entities to an attribute, use `setAttributeNode` or `setAttributeNodeNS`.
Throws
    Raises an INVALID_CHARACTER_ERR error if the supplied name contains an invalid character.
    Raises a NO_MODIFICATION_ALLOWED_ERR error if the node is read-only.
See Also

  Element.getAttribute|Element.removeAttribute|Element.setAttributeNode

Availability

  HTML DOM Level 2|W3C

static setAttributeNode(Attr newAttr) : Attr Adds the given attribute node | Show Details | 4.0+ | 1.0+ | 6.0+ | 8.0+ | 1.0+

- Safari: Safari will create the attribute, but you will have to use nodeValue to set its value.

Parameters

Attr | newAttr | Attr node of the attribute to set.

Returns

Attr |

  ### Using setAttributeNode

      var attr = node1.getAttributeNode("class");
                              node1.removeAttribute("class");
                              node2.setAttributeNode(attr);

  For more examples, see the quirksmode test page:

  http://www.quirksmode.org/dom/tests/attributes3.html

Remarks

  This method adds the specified `Attr` object as an attribute to the current element. You may specifiy actual DOM nodes instead of specifying separate arguments for the attribute name and value.

  If the element already has the attribute, `setAttribute` will replace the node and return the old attribute node. Returns null, otherwise.

Throws
    Raises a WRONG_DOCUMENT_ERR error if the supplied newAttr was created from a different document.
    Raises a NO_MODIFICATION_ALLOWED_ERR error if the node is read-only.
    Raises an INUSE_ATTRIBUTE_ERR error if the supplied newAttr is already bound to another Element object.
See Also

  Attr|Element.setAttribute

Availability

  HTML DOM Level 2|W3C

static setAttributeNodeNS(Attr newAttr) : Attr Sets an attribute value with the given node's name and namespace URI. | Show Details | no | 1.0+ | 6.0+ | 8.0+ | no

Parameters

Attr | newAttr | Attr node of the attribute to set, including the namespace information.

Returns

Attr |

Remarks

  Like it's non-namespace aware counterpart <method>setAttributeNode, this method sets or replaces an attribute in an `Element` node by supplying the actual `Attr` node that is to be bound to the object. Unlike <method>setAttributeNode, this method is aware of, and can set, the attribute using the localName and namespaceURI.

  If an attribute already exists in this `Element` with the supplied attribute name properties, then it is replaced, and the old attribute is returned. Otherwise, `null` is returned.

Throws
    Raises a WRONG_DOCUMENT_ERR error if the supplied newAttr was created from a different document.
    Raises a NO_MODIFICATION_ALLOWED_ERR error if the node is read-only.
    Raises an INUSE_ATTRIBUTE_ERR error if the supplied newAttr is already bound to another Element object.
See Also

  Attr|Element.setAttributeNS|Element.setAttributeNode

Availability

  HTML DOM Level 2|W3C

setAttributeNS(String namespaceURI, String qualifiedName, String value) : void Adds a new attribute with a namespace URI | Show Details | no | 1.0+ | 6.0+ | 8.0+ | no

Parameters

String | namespaceURI | Namespace URI of the attribute to set.
String | qualifiedName | Fully qualified name of the attribute to set.
String | value | String value of the attribute to set.

Returns

void |

  ### Using setAttributeNS

      node.setAttributeNS(
                              "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
                              "rdf:about",
                              "urn:root"
                              );

Remarks

  This method, similar to the <method>setAttribute method, sets a new attribute value. If the attribute already exists, it's value is set to the one supplied to this method. This version of the method however takes both a <glossary>qualified name</glossary> and <glossary>namespace URI</glossary> when setting the attribute. This permits the creation of attributes defined with namespaces.

  Like <method>setAttribute, this method accepts only plain string values, and as such cannot be used for defining entity references within strings. Any entity reference will be escaped prior to being set with the attribute. If you would like to assign values containing entities to an attribute, please see <method>setAttributeNode or <method>setAttributeNodeNS.

Throws
    Raises an INVALID_CHARACTER_ERR error if the supplied name contains an invalid character.
    Raises a NO_MODIFICATION_ALLOWED_ERR error if the node is read-only.
    Raises a NAMESPACE_ERR error if the qualifiedName is invalid, if the qualifiedName contains a namespace prefix yet no namespaceURI is supplied, if the namespace prefix is "xml" or "xmlns" and has a namespaceURI that does not match the prefix.
See Also

  Element.setAttribute|Element.setAttributeNode

Availability

  HTML DOM Level 2|W3C

## Remarks

One of the most critical objects within the DOM, the `Element` object represents an individual element within an XML or HTML document. Due to the capabilities of XML elements, this object provides several methods for handling attributes.

The attribute manipulation methods can be divided into three different groups: plain methods, node-oriented methods, and namespace-aware methods. The first category is the simplest, and permits both the attribute name and value to be supplied to the method, thus creating an `Attr` under the covers. The second category enables a user to create an `Attr` themselves, and merely to attach it to the `Element` object. The final category is similar to the first, except an extra argument can be supplied which allows you to create an attribute with an XML namespace.

Many of the capabilities of this object is inherited from `Node`, since they share many similarities. Therefore, this object exposes methods to get at all the available attributes (since it inherits the attributes property), individual attribute values, or individual `Attr` objects.

## References

HTMLElement|Node

## Availability

HTML DOM Level 2|W3C

text_javascript aptana_docs