JavaScript Element removeAttributeNS() Method

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

What You’ll Learn

Element.removeAttributeNS() is an instance method that removes a namespaced attribute from an element. Learn MDN’s specialAlign example, SVG xlink:href removal, when to prefer removeAttribute() for plain HTML, pairing with hasAttributeNS(), and five try-it labs aligned with MDN.

01

Kind

Instance method

02

Returns

undefined

03

Args

URI + attrName

04

Missing

no error

05

HTML

removeAttribute

06

Status

Baseline widely

Introduction

Most HTML attributes are removed with removeAttribute("class"). When an attribute belongs to a namespace — common in SVG and XML — you need removeAttributeNS(namespace, attrName) instead.

MDN: if you work with HTML documents and do not need to specify the attribute as part of a namespace, use removeAttribute(). Reach for removeAttributeNS() when namespace URIs matter.

💡
Beginner tip

MDN names the second parameter attrName. For xlink:href, use namespace http://www.w3.org/1999/xlink and attribute name "href" (local name). Store namespace URIs in constants like XLINK_NS.

This page is part of JavaScript Element. Related: removeAttribute(), hasAttributeNS().

Understanding the removeAttributeNS() Method

Calling el.removeAttributeNS(namespace, attrName) removes the namespaced attribute that matches the namespace URI and attribute name.

  • It is an instance method on Element.
  • namespace — a string with the attribute namespace URI (MDN).
  • attrName — a string naming the attribute to remove (MDN).
  • Returns undefined (MDN).
  • Does not throw when the attribute is missing (same pattern as removeAttribute()).
  • For plain HTML attributes, prefer removeAttribute() (MDN).
  • Pair with hasAttributeNS() to verify removal.

📝 Syntax

General form of Element.removeAttributeNS (MDN):

JavaScript
removeAttributeNS(namespace, attrName)

Parameters

namespace — a string specifying the namespace of the attribute (MDN).

attrName — a string that names the attribute to be removed from the element (MDN).

Return value

None (undefined) (MDN).

Common patterns

JavaScript
const XLINK_NS = "http://www.w3.org/1999/xlink";
const SPECIAL_NS = "http://www.mozilla.org/ns/specialspace";

// MDN example
const d = document.getElementById("div1");
d.removeAttributeNS(SPECIAL_NS, "specialAlign");

// SVG xlink:href removal
const anchor = document.querySelector("svg a");
anchor.removeAttributeNS(XLINK_NS, "href");

// Plain HTML? Prefer removeAttribute:
// el.removeAttribute("class");

⚡ Quick Reference

GoalCode
Namespaced removeel.removeAttributeNS(XLINK_NS, "href")
XLink namespace"http://www.w3.org/1999/xlink"
Plain HTMLel.removeAttribute("class")
Verify afterel.hasAttributeNS(ns, "href")
Missing attributeNo error (no-op)
MDN statusBaseline Widely available (since July 2015)

🔍 At a Glance

Four facts to remember about Element.removeAttributeNS().

Returns
undefined

No return value

Baseline
widely

Since Jul 2015

Args
URI+name

Namespace

HTML
removeAttr

Simpler

📋 removeAttributeNS() vs removeAttribute()

removeAttributeNS()removeAttribute()
Returnsundefinedundefined
Parametersnamespace + attrNameattribute name
PurposeRemove namespaced attrsRemove plain HTML attrs
HTML docsSVG/XML namespacesDefault choice (MDN)
Missing attributeNo errorNo error (MDN)
Best forSVG/XML toolingEveryday attribute delete

Examples Gallery

Examples follow MDN Element.removeAttributeNS() patterns. Use View Output or Try It Yourself for each case.

📚 Getting Started

MDN’s namespaced attribute removal and SVG xlink.

Example 1 — Remove specialAlign (MDN)

MDN: remove a custom namespaced attribute from a div.

JavaScript
// Given: <div id="div1" xmlns:special="http://www.mozilla.org/ns/specialspace"
//   special:specialAlign="utterleft" width="200px" />
const SPECIAL_NS = "http://www.mozilla.org/ns/specialspace";
const d = document.getElementById("div1");

d.removeAttributeNS(SPECIAL_NS, "specialAlign");
// specialAlign is now removed
Try It Yourself

How It Works

Pass the namespace URI and attribute name "specialAlign" exactly as MDN documents. The namespaced attribute is detached from the element.

📈 Practical Patterns

Plain HTML contrast, verification, and safe no-ops.

Example 3 — Prefer removeAttribute() for HTML

MDN: skip namespaces when deleting ordinary HTML attributes.

JavaScript
const el = document.getElementById("box");

// Namespaced API (only when you need a namespace URI)
// el.removeAttributeNS(SOME_NS, "attrName");

// Simpler for plain HTML (MDN recommendation)
el.removeAttribute("data-role");

console.log(el.hasAttribute("data-role"));
Try It Yourself

How It Works

Both methods delete attributes. Use removeAttribute() unless the attribute is explicitly namespaced (SVG/XML).

Example 4 — Verify with hasAttributeNS()

Confirm the namespaced attribute is gone after removal.

JavaScript
const XLINK_NS = "http://www.w3.org/1999/xlink";
const anchor = document.querySelector("svg a");

const before = anchor.hasAttributeNS(XLINK_NS, "href");
anchor.removeAttributeNS(XLINK_NS, "href");
const after = anchor.hasAttributeNS(XLINK_NS, "href");

console.log(before, after);
Try It Yourself

How It Works

Pair removal with hasAttributeNS() to confirm the attribute no longer exists on the element.

Example 5 — Safe When Attribute Is Missing

Like removeAttribute(), removing a missing namespaced attr does not throw.

JavaScript
const XLINK_NS = "http://www.w3.org/1999/xlink";
const anchor = document.querySelector("svg a");

// Already removed or never present — still safe
anchor.removeAttributeNS(XLINK_NS, "href");
anchor.removeAttributeNS(XLINK_NS, "href");

console.log("no error");
Try It Yourself

How It Works

You can call removeAttributeNS() without a prior existence check. If the attribute is not there, the call completes without throwing.

🚀 Common Use Cases

  • Removing SVG xlink:href from icon and chart elements.
  • Stripping custom XML namespace attributes in mixed documents.
  • DOM libraries paired with setAttributeNS() for round-trip edits.
  • Cleaning namespaced hooks before cloning or serializing SVG nodes.
  • Teaching when namespace URIs matter vs plain removeAttribute().
  • Verifying removal with hasAttributeNS() in tests and tooling.

🧠 How removeAttributeNS() Removes Namespaced Attributes

1

Pass namespace + attrName

removeAttributeNS(XLINK_NS, "href")

Args
2

Find namespaced attribute

Browser matches namespace URI and attribute name (MDN).

Lookup
3

Detach attribute

The namespaced attribute is removed from the element.

Remove
4

Verify if needed

Use hasAttributeNS() to confirm the attribute is gone.

📝 Notes

  • Not Deprecated, Experimental, or Non-standard on MDN (Baseline Widely available, July 2015).
  • For plain HTML attributes, MDN recommends removeAttribute() instead.
  • Uses namespace URI + attrName (MDN parameter name).
  • Returns undefined — no removed value is returned.
  • Does not throw when the attribute is missing (safe no-op).
  • Pair with hasAttributeNS() to verify removal.
  • Related: removeAttribute(), hasAttributeNS(), getAttributeNS(), JavaScript hub.

Browser Support

Element.removeAttributeNS() is Baseline Widely available (MDN: across browsers since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline Widely available

Element.removeAttributeNS()

Remove a namespaced attribute from an element — namespace URI plus attribute name.

Baseline Widely available
Google Chrome Supported · Desktop & Mobile
Yes
Microsoft Edge Supported · Chromium
Yes
Mozilla Firefox Supported · Desktop & Mobile
Yes
Apple Safari Supported · macOS & iOS
Yes
Opera Supported · Modern versions
Yes
Internet Explorer Supported in legacy IE
Yes
removeAttributeNS() Excellent

Bottom line: Use removeAttributeNS() for SVG and XML namespaced attributes. For ordinary HTML attributes, removeAttribute() is simpler (MDN).

Conclusion

Element.removeAttributeNS() deletes a namespaced attribute from an element. It is the namespace-aware companion to removeAttribute() and pairs naturally with hasAttributeNS() for verification.

Continue with removeAttribute(), hasAttributeNS(), replaceChildren(), shadowRoot, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Store namespace URIs in constants (XLINK_NS)
  • Use attrName per MDN (e.g. "href" for xlink)
  • Prefer removeAttribute() for plain HTML attributes
  • Verify with hasAttributeNS() after removal
  • Use for SVG/XML namespaced attribute deletion

❌ Don’t

  • Use for ordinary HTML when removeAttribute() suffices
  • Pass "xlink:href" as attrName (use "href")
  • Expect a return value from the method
  • Hard-code wrong namespace URIs
  • Assume removeAttribute("href") removes xlink attrs

Key Takeaways

Knowledge Unlocked

Five things to remember about Element.removeAttributeNS()

Namespace-aware attribute removal.

5
Core concepts
📄 02

Args

URI + name

Two
✍️ 03

SVG

xlink href

Common
04

HTML

removeAttr

Simpler
05

Pair

hasAttrNS

Verify

❓ Frequently Asked Questions

It removes the specified attribute with the specified namespace from an element (MDN).
No. MDN marks Element.removeAttributeNS() as Baseline Widely available (across browsers since July 2015). It is not Deprecated, Experimental, or Non-standard.
namespace — a string with the attribute namespace URI. attrName — a string naming the attribute to remove (MDN uses the local name, e.g. specialAlign).
MDN: if you work with HTML and do not need to specify the attribute as part of a namespace, use removeAttribute(attrName) instead.
Nothing useful — the return value is undefined (MDN).
Use hasAttributeNS(namespace, localName) after removing. It should return false when the namespaced attribute is gone.
Did you know?

MDN: for HTML documents where you do not need a namespace, use removeAttribute(). Reach for removeAttributeNS() when the attribute belongs to a specific namespace URI such as XLink in SVG.

More Element Topics

Browse Element methods and properties to keep building your DOM skills.

replaceChildren() →

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.

8 people found this page helpful