createAttribute (W3C DOM Core method)

Version Depr. Static
DOM1 No No
Browser support (more…)
IE6+ FF1.5+ SA1.3+ OP9+
Full Full Full Full


var attr = document.createAttribute('href');

attr.nodeValue = '';


The example above creates an href attribute, assigns it the nodeValue and then adds it to an element using setAttributeNode.

So if the element in question were an HTML anchor element, like this:


The operation above would result in this:

<a href="">SitePoint</a>


name (DOMString) required

The name of the attribute.


Create an Attr node of the specified name.

The created attribute can then be added to an Element using the setAttributeNode method.

A value can be assigned to it using the specialized value property of Attr, or the more general nodeValue property of Node. The value is not parsed, so any entity references or other markup will be treated as literal text. To create an attribute containing entities the specification suggests to create an Attr node with appropriate Text and EntityReference nodes as children, then add it to an element using setAttributeNode, however in practise this rarely works (see Attr for details).

This method creates non-namespaced attributes; to create a namespaced attribute, use the DOM 2 createAttributeNS method instead.

Return value

The created attribute node, with its nodeName set to the specified name, its localName, prefix and namespaceURI set to null (coming soon!), and an empty string value


Internet Explorer Firefox Safari Opera
5.5 6.0 7.0 1.5 2.0 3.0 1.3 2.0 3.0 9.0 9.5
Partial Full Full Full Full Full Full Full Full Full Full

Internet Explorer 5.5 in HTML doesn't implement this method (it returns undefined).

: Associated behaviors

The following notes relate to additional functionality that's needed to make this method useful in practise, rather than being about the method itself; the support table above does not take account of these notes:

Opera 9.0 in pure XML (but not XHTML mode1) parses entities when setting the value or nodeValue of an attribute node.

In Safari 1.3 and 2 it's not possible to set the value of a created attribute using the value property (the property is read-only; however using the nodeValue property is supported fine).


1 On XHTML pages served as application/xhtml+xml.

User-contributed notes

There are no comments yet.

Related Products