HTML DOM Attribute value
Examples
Get the value of the first attribute:
let value = element.attributes[0].value;
Try it Yourself »
Get the value of the "id" attribute:
let value = element.getAttributeNode("id").value;
Try it Yourself »
More examples below.
Definition and Usage
The value
property sets or returns the value of an attribute.
Syntax
Return the attribute value:
attribute.value
Set the attribute value:
attribute.value = value
Property
Property | Description |
value | The value of the attribute. |
Return Value
Type | Description |
A string | The value of the attribute. |
More Examples
Change the value of the src attribute of an image:
Use the getNamedItem() method:
const nodeMap = document.getElementById("light").attributes;
let value = nodeMap.getNamedItem("src").value;
Try it Yourself »
Use the getAttributeNode() method:
const element = document.getElementById("light");
element.getAttributeNode("src").value = "pic_bulbon.gif";
Try it Yourself »
Browser Support
attribute.value
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 |