Document: createAttribute() method - Web APIs | MDN

Syntax

Parameters

name

A string containing the name of the attribute.

Return value

An Attr node.

Exceptions

InvalidCharacterError DOMException

Thrown if the name value is not a valid XML name; for example, it starts with a number, hyphen, or period, or contains characters other than alphanumeric characters, underscores, hyphens, or periods.

Examples

js

const node = document.getElementById("div1");
const a = document.createAttribute("my_attrib");
a.value = "newVal";
node.setAttributeNode(a);
console.log(node.getAttribute("my_attrib")); // "newVal"

Specifications

Specification
DOM
# dom-document-createattribute

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on by MDN contributors.