JavaScript Document createAttributeNS() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

document.createAttributeNS() creates a new namespaced attribute node (Attr) with a namespaceURI and qualifiedName. Creating the node does not attach it — set value, then use setAttributeNode() (MDN).

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call document.createAttributeNS. Start with this line.

JavaScript
document.createAttributeNS(namespaceURI, qualifiedName)
CallWhat it does
document.createAttributeNS(namespaceURI, qualifiedName)Primary signature.
document.createAttributeNS(...)Call with the arguments your case needs.

Built into the browser. Call createAttributeNS(...) on document — no import required.

Library declarations
JavaScript
document.createAttributeNS(namespaceURI, qualifiedName)

Parameters

ParameterDescription
namespaceURIRequired. Namespace string, or "" for null
qualifiedNameRequired. prefix:localName or localName
Attach stepRequired. setAttributeNode / setAttributeNodeNS

Return Value

Return ValueDescription
*Attr node

JavaScript Examples

Example 1 — Create xml:lang

MDN pattern: XML namespace + xml:lang, then attach.

JavaScript
const el = document.createElement("p");
const a = document.createAttributeNS(
  "http://www.w3.org/XML/1998/namespace",
  "xml:lang"
);
a.value = "fr";
el.setAttributeNode(a);

console.log(el.getAttribute("xml:lang")); // "fr"
Try It Yourself

How It Works

  1. Pick the XML URI. Required when the prefix is xml.
  2. Create the Attr. qualifiedName is "xml:lang".
  3. Set the value. Assign a.value = "fr".
  4. Attach. setAttributeNode(a) puts it on the element.

Example 2 — Unprefixed viewBox

Null namespace: pass "" for unprefixed attributes like SVG viewBox (MDN).

JavaScript
const svg = document.createElementNS(
  "http://www.w3.org/2000/svg",
  "svg"
);
const a = document.createAttributeNS("", "viewBox");
a.value = "0 0 100 100";
svg.setAttributeNode(a);

console.log(svg.getAttribute("viewBox")); // "0 0 100 100"
Try It Yourself

How It Works

  1. Create an SVG host. Use createElementNS for the <svg>.
  2. Pass empty ns. "" means the null namespace (MDN).
  3. Name is unprefixed. "viewBox" has no prefix:.
  4. Simpler path. Everyday code can use svg.setAttribute("viewBox", …) instead.

Example 3 — NamespaceError

A prefix with an empty namespace URI throws NamespaceError (MDN).

JavaScript
try {
  document.createAttributeNS("", "xml:lang");
  console.log("ok");
} catch (err) {
  console.log(err.name); // "NamespaceError"
}
Try It Yourself

How It Works

  1. Prefix needs a URI. xml: is a prefix, so ns cannot be "".
  2. Call throws. The engine raises NamespaceError.
  3. Catch err.name. Expect "NamespaceError".
  4. Fix it. Use the XML URI, or drop the prefix for null-ns names.

Browser Support

Document.createAttributeNS() is Baseline Widely available (MDN). It is not Deprecated, Experimental, or Non-standard.

✓ Baseline · Widely available

Document.createAttributeNS()

Supported in every modern browser. Prefer setAttributeNS for everyday namespaced sets.

99% Modern browser support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
document.createAttributeNS() Excellent

Bottom line: Safe everywhere. Focus on correct namespace URIs and when you need an Attr node versus setAttributeNS.

Frequently Asked Questions

MDN: Document.createAttributeNS() creates a new attribute node with a specified namespace URI and qualified name. The result is an Attr object — set attr.value, then attach with setAttributeNode() or setAttributeNodeNS().
No. MDN marks Document.createAttributeNS() as Baseline Widely available. It is not Deprecated, Experimental, or Non-standard.
Use createAttributeNS() when the attribute belongs to a namespace — for example xml:lang (XML namespace) or xlink:href (XLink). For ordinary HTML attributes in the null namespace, createAttribute() or setAttribute() is enough.
MDN: in HTML documents most attributes are in the null namespace — pass the empty string "" for namespaceURI when creating unprefixed attributes like SVG viewBox.
MDN: a string like prefix:localName or just localName. For xml:lang, qualifiedName is "xml:lang" and namespaceURI is http://www.w3.org/XML/1998/namespace.
Yes. For most apps, element.setAttributeNS(namespace, name, value) is simpler than createAttributeNS + attach. Use createAttributeNS when you need an Attr node object.

Next: createCDATASection()

Learn how to create CDATA section nodes for XML-style content.

createCDATASection() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful