JavaScript Document createAttributeNS() Method

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Instance method

What You’ll Learn

document.createAttributeNS() is an instance method that creates a namespaced Attr node (see MDN Document: createAttributeNS()). Learn namespaceURI, qualifiedName, MDN’s xml:lang and SVG viewBox examples, how it compares to createAttribute() and setAttributeNS(), and five try-it labs.

01

Kind

Instance method

02

Args

URI + name

03

Returns

Attr node

04

Use for

namespaces

05

HTML null NS

"" URI

06

Status

Baseline

Introduction

createAttribute() builds attribute nodes for the default (null) namespace. When an attribute name includes a prefix tied to a namespace — like xml:lang or xlink:href — you need the namespaced version: createAttributeNS(namespaceURI, qualifiedName).

MDN: the method returns an Attr node. Set attr.value, then attach it with setAttributeNode() or setAttributeNodeNS().

💡
Common namespace URIs (MDN)

XML: http://www.w3.org/XML/1998/namespace (xml:lang)
XMLNS: http://www.w3.org/2000/xmlns/ (xmlns)
XLink: http://www.w3.org/1999/xlink (xlink:href)

Related tutorials: createAttribute(), setAttributeNS(), setAttributeNodeNS().

Understanding document.createAttributeNS()

An instance method on the page’s document object. Part of the DOM Core API (MDN).

  • namespaceURI — namespace string, or "" for null namespace (MDN).
  • qualifiedNameprefix:localName or localName (MDN).
  • Return value — a new Attr node (MDN).
  • AttachsetAttributeNode / setAttributeNodeNS.
  • Validation — invalid URI or name parts throw (MDN).
  • DOM freedom — does not enforce valid element/attribute pairing (MDN).

📝 Syntax

General form of Document.createAttributeNS (MDN):

JavaScript
createAttributeNS(namespaceURI, qualifiedName)

Parameters

  • namespaceURI — namespace to associate with the attribute, or empty string (MDN).
  • qualifiedName — qualified name string; initializes name on the Attr (MDN).

Return value

A new Attr node (MDN).

MDN: xml:lang example

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

MDN: unprefixed attribute (null namespace)

JavaScript
const svg = document.getElementById("svg");
const attr = document.createAttributeNS("", "viewBox");
attr.value = "0 0 100 100";
svg.setAttributeNode(attr);
console.log(svg.getAttribute("viewBox")); // "0 0 100 100"

⚡ Quick Reference

GoalCode
xml:langcreateAttributeNS(XML_NS, "xml:lang")
Null namespacecreateAttributeNS("", "viewBox")
Set valueattr.value = "fr"
Attachel.setAttributeNode(attr)
Simpler setel.setAttributeNS(uri, qn, val)
MDN statusBaseline Widely available

🔍 At a Glance

Four facts about document.createAttributeNS().

Returns
Attr

node

Args
URI + qName

two

HTML
"" URI

null NS

Prefer
setAttributeNS

usually

📋 Namespace URI quick map (MDN)

AttributenamespaceURIqualifiedName
xml:langhttp://www.w3.org/XML/1998/namespacexml:lang
xml:spacehttp://www.w3.org/XML/1998/namespacexml:space
xmlnshttp://www.w3.org/2000/xmlns/xmlns
xlink:hrefhttp://www.w3.org/1999/xlinkxlink:href
viewBox (SVG)"" (null)viewBox

Examples Gallery

Examples follow MDN Document: createAttributeNS() and show namespaced attribute patterns.

📚 Getting Started

Create namespaced Attr nodes from MDN examples.

Example 1 — MDN: xml:lang on a paragraph

Specify French language metadata with the XML namespace.

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

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

How It Works

Both the XML namespace URI and the xml:lang qualified name are required (MDN).

Example 2 — MDN: SVG viewBox (null namespace)

Unprefixed SVG attributes use an empty string for namespaceURI (MDN).

JavaScript
const svg = document.getElementById("svg");
const attr = document.createAttributeNS("", "viewBox");
attr.value = "0 0 100 100";
svg.setAttributeNode(attr);

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

How It Works

MDN: in HTML documents, unprefixed attributes like SVG viewBox are in the null namespace.

📈 Practical Patterns

Inspect nodes, compare APIs, and use simpler alternatives.

Example 3 — Read namespaceURI, prefix, localName

Inspect properties on the created Attr node.

JavaScript
const attr = document.createAttributeNS(
  "http://www.w3.org/XML/1998/namespace",
  "xml:lang"
);

console.log({
  name: attr.name,
  namespaceURI: attr.namespaceURI,
  prefix: attr.prefix,
  localName: attr.localName
});
Try It Yourself

How It Works

qualifiedName splits into prefix and localName on the node (MDN).

Example 4 — Simpler: setAttributeNS() (MDN note)

MDN: for unprefixed attributes you can often use setAttribute instead.

JavaScript
const svg = document.getElementById("svg");

// Attr node path
// const attr = document.createAttributeNS("", "viewBox");
// attr.value = "0 0 100 100";
// svg.setAttributeNode(attr);

// One-liner (MDN recommendation for unprefixed):
svg.setAttribute("viewBox", "0 0 100 100");

// Namespaced one-liner:
svg.setAttributeNS(
  "http://www.w3.org/1999/xlink",
  "xlink:href",
  "#icon"
);
Try It Yourself

How It Works

Use createAttributeNS when you need the Attr object; otherwise prefer string setters.

Example 5 — createAttribute vs createAttributeNS

Plain HTML title vs namespaced xml:lang.

JavaScript
const p = document.createElement("p");
p.textContent = "Hello";

// Null namespace — createAttribute is enough
const title = document.createAttribute("title");
title.value = "Hint";
p.setAttributeNode(title);

// Namespaced — need createAttributeNS
const lang = document.createAttributeNS(
  "http://www.w3.org/XML/1998/namespace",
  "xml:lang"
);
lang.value = "en";
p.setAttributeNode(lang);

console.log({
  title: p.getAttribute("title"),
  lang: p.getAttribute("xml:lang")
});
Try It Yourself

How It Works

Choose createAttributeNS only when a namespace URI is involved.

🚀 Common Use Cases

  • XML / SVG tooling — build namespaced attributes programmatically.
  • xml:lang / xml:space — XML namespace metadata on elements (MDN).
  • XLink attributesxlink:href on SVG or mixed documents.
  • Attr node pipelines — clone or move namespaced attrs with node APIs.
  • Teaching namespaces — show URI + qualifiedName on DOM nodes.
  • Everyday HTML — prefer setAttribute / setAttributeNS.

🧠 How createAttributeNS() Works

1

Pass URI + qualifiedName

MDN: namespace URI (or "") and a qualified name string.

Input
2

Create detached Attr

Browser returns a namespaced attribute node with prefix/localName set.

Create
3

Assign attr.value

Set the attribute value before attaching to an element.

Value
4

Attach to element

setAttributeNode or setAttributeNodeNS; read with getAttribute.

📝 Notes

  • MDN: Baseline Widely available since July 2015.
  • Use "" for null namespace unprefixed attributes (MDN).
  • Invalid namespace URI or name parts throw (MDN).
  • If prefix is xml or xmlns, URI must match MDN rules.
  • DOM does not validate element/attribute pairing (MDN).
  • Related: createAttribute(), setAttributeNS(), setAttributeNodeNS().

Browser Support

Document.createAttributeNS() is Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline Widely available

Document.createAttributeNS()

Creates namespaced Attr nodes — supported across all major browsers.

Baseline Widely available
Google Chrome Supported
Yes
Mozilla Firefox Supported
Yes
Apple Safari Supported
Yes
Microsoft Edge Supported
Yes
Opera Supported
Yes
Internet Explorer Supported (legacy)
Yes
createAttributeNS() Wide

Bottom line: Use createAttributeNS for namespaced Attr nodes; prefer setAttributeNS for simple name/value updates.

Conclusion

document.createAttributeNS(namespaceURI, qualifiedName) creates a namespaced Attr node. Use it for attributes like xml:lang or when you need the node object before attaching. For most day-to-day updates, setAttributeNS() is simpler.

Continue with createCDATASection(), setAttributeNS(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use correct namespace URI for prefixed names (MDN)
  • Pass "" for null-namespace unprefixed attrs
  • Prefer setAttributeNS for simple updates
  • Set attr.value before attaching
  • Pair with setAttributeNodeNS when moving Attr nodes

❌ Don’t

  • Use createAttributeNS for plain HTML when setAttribute suffices
  • Mix wrong URI with xml / xmlns prefix (MDN)
  • Assume DOM validates attribute/element pairs (MDN)
  • Forget qualifiedName format prefix:localName
  • Confuse with createElementNS (creates elements)

Key Takeaways

Knowledge Unlocked

Five things to remember about createAttributeNS()

Namespaced Attr nodes for XML, SVG, and XLink.

5
Core concepts
🌐02

xml:lang

XML NS

example
📄03

Null NS

"" URI

viewBox
04

Prefer

setAttributeNS

daily
🛡05

Status

Baseline

2015

❓ 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 you can attach with setAttributeNode() or setAttributeNodeNS().
No. MDN marks Document.createAttributeNS() as Baseline Widely available (since July 2015). 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 + setAttributeNodeNS. Use createAttributeNS when you need an Attr node object.
Did you know?

MDN’s SVG viewBox example uses createAttributeNS("", "viewBox") — an empty namespace URI — and notes that in most cases you can skip the Attr node entirely and call svg.setAttribute("viewBox", "0 0 100 100") instead.

Next: createCDATASection()

Learn how to create CDATA Section nodes for XML documents with DOMParser.

createCDATASection() →

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.

6 people found this page helpful