HTML DOM Attributes getNamedItem()
Example
Get the value of the scr attribute of an image:
const nodeMap = document.getElementById("light").attributes;
let value = nodeMap.getNamedItem("src").value;
Try it Yourself »
Get the value of the onclick attribute of a button:
const nodeMap = document.getElementById("myButton");
let value = nodeMap.getNamedItem("onclick").value;
Try it Yourself »
Definition and Usage
The getNamedItem()
method returns an attribute node from a NamedNodeMap object.
Alternative:
It is easier to use the element.getAttribute() method.
See Also:
The attributes.setNamedItem() Method
Syntax
namednodemap.getNamedItem(nodename)
Parameters
Parameter | Description |
nodename | Required. The name of the node in the namedNodeMap. |
Return Value
Type | Description |
A node | The node with the specified name. |
Browser Support
attributes.getNamedItem
is a DOM Level 1 (1998) feature.
It is fully supported in all browsers:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |