setNamedItem (W3C DOM Core method)
| Version | Depr. | Static |
|---|---|---|
| DOM1 | No | No |
| IE5.5+ | FF1.5+ | SA1.3+ | OP9+ |
|---|---|---|---|
| Buggy | Full | Full | Full |
- Returns
Node- Throws
WRONG_DOCUMENT_ERR,NO_MODIFICATION_ALLOWED_ERR,INUSE_ATTRIBUTE_ERR
Example
var attrs = document.getElementsByTagName('link').item(0).attributes;
var media = document.createAttribute('media');
media.nodeValue = 'all';
attrs.setNamedItem(media);
The example above saves the attributes collection of an HTML
link element, then creates a new attribute node with
the name media and the value all,
and assigns it to that collection.
If a node with that name already
exists in the collection then it will be overwritten with the new value;
otherwise a new item will be added, and therefore a new attribute node
will apply to the original link element.
Arguments
Description
Add a specified node to a
NamedNodeMap.
If a node with that name
already exists in the map, it will be replaced. For certain kinds of node
— where all instances have the same name (such as #text
or #comment) — it's not possible to store more than one
instance of such a node within a single node map, because the names would
clash. The DOM specification considers this preferable to implementing
aliases.
Return value
If the new node replaces an
existing node, the previously existing node is returned;
otherwise null is returned.
Compatibility
| 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 |
| Buggy | Buggy | Buggy | Full | Full | Full | Full | Full | Full | Full | Full |
Internet Explorer 5.5 in HTML doesn't implement this method (it returns undefined).
In Internet
Explorer 6 and 7 in HTML this method does not always return
null when adding to an attributes collection where the
specified node didn't previously exist; it returns
null, 0, false or
an empty string, depending on what type of value IE considers the
attribute to have (please see Attr for more
information on attribute types in IE).
Internet
Explorer in XML returns the newly set value, rather than the
previously set value or null.
As an added conundrum, the MSDN documentation for this method
says that "an attribute that is set with this method does not have
to apply to the element". However in tests, created attributes
added to an attributes collection do
indeed apply to their owning element, so I have no idea what situation
they might be talking about.
User-contributed notes
There are no comments yet.
Add a note
To post a note on this topic, please log in with your SitePoint username and password. If you don't have an account yet, you can create a new account for free.