HTML DOM Attribute value Property

Get the value of the first attribute

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 stringThe value of the attribute.

function lightOn() { const element = document.getElementsByTagName("IMG")[0]; const attr = element.getAttributeNode("src"); attr.value = "pic_bulbon.gif"; }

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