GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • MDN
  • HTML: Markup language
  • Elements
  • Global attributes
  • Attributes
  • See all…
  • Responsive images
  • HTML cheatsheet
  • Date & time formats
  • See all…
  • SVG
  • MathML
  • XML
  • CSS: Styling language

This view

  • Original page
  • New address

GeezerVison is showing developer.mozilla.org — Document: createAttribute() method - Web APIs | MDN

Document: createAttribute() method

Baseline
Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

  • See full compatibility
  • Learn more

The createAttribute() method of the Document interface creates a new attribute node.

The object created is a node implementing the Attr interface. The DOM does not enforce what sort of attributes can be added to a particular element in this manner.

Note: The string given in parameter is converted to lowercase.

Syntax

js
createAttribute(localName)

Parameters

localName

A string containing the name of the attribute. The value is used to initialize the new attribute's localName property.

Return value

An Attr node.

Exceptions

InvalidCharacterError DOMException

Thrown if the localName value is not a valid attribute name. It must have at least one character, and may not contain ASCII whitespace, NULL, /, = or > (U+0000, U+002F, U+003D, or U+003E, respectively).

Note: Earlier versions of the specification were more restrictive, requiring that the localName be a valid XML name.

Examples

Basic example

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

  • Document.createAttributeNS()
  • Document.createElement()
  • Element.setAttribute()
  • Element.setAttributeNode()

Help improve MDN

Learn how to contribute

This page was last modified on Apr 6, 2026 by MDN contributors.

View this page on GitHub • Report a problem with this content
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.