CKTIS / JavaScript
Element.getAttribute — reading an attribute value
Element.getAttribute reads a DOM element's attribute as a string, or returns null when that attribute does not exist.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
element.getAttribute(qualifiedName)element.getAttribute(qualifiedName) returns the value of the first attribute with the given qualified name. The result is a string, or null if the attribute does not exist. An empty value is "", not null; the text "12" is not a number. The method does not modify the element.
For elements in the HTML namespace belonging to an HTML document, the name is converted to ASCII lowercase before lookup. Outside that case, letter case matters. Use getAttributeNS(namespace, localName) to look up an attribute by namespace and local name.
Examples
const element = document.createElement('div');
element.setAttribute('data-count', '12');
element.setAttribute('data-empty', '');
console.log(JSON.stringify([
element.getAttribute('data-count'),
element.getAttribute('data-empty'),
element.getAttribute('data-missing')
]));Result
["12","",null]
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.