JavaScript Element removeAttributeNode() Method

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

What You’ll Learn

Element.removeAttributeNode() is an instance method that removes an Attr node from an element and returns that node. Learn the MDN lang workflow with getAttributeNode(), when to prefer removeAttribute(), and five try-it labs aligned with MDN.

01

Kind

Instance method

02

Arg

Attr node

03

Returns

removed Attr

04

Error

NotFoundError

05

vs removeAttr

name vs node

06

Status

Baseline widely

Introduction

Most code deletes attributes with removeAttribute("lang"). The removeAttributeNode() variant takes an Attr node instead of a name. MDN: it removes the specified Attr node from the element.

Use it when you already have an Attr object—for example from getAttributeNode()—and want to inspect it before removal or keep the removed node in a variable.

💡
Beginner tip

MDN: if you do not need to inspect the attribute node first, use removeAttribute() instead. It is simpler for everyday tasks.

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

Understanding the removeAttributeNode() Method

Calling el.removeAttributeNode(attrNode) detaches that Attr from the element and returns the removed node.

  • It is an instance method on Element.
  • Parameter: an Attr node that belongs to this element (MDN).
  • Returns the removed Attr node (MDN).
  • Throws NotFoundError if the Attr is not in this element’s list (MDN).
  • Works for both namespaced and non-namespaced attributes (MDN notes).
  • If the attribute has a default value, it may be immediately replaced (MDN).
  • Pair with getAttributeNode() and setAttributeNode().

📝 Syntax

General form of Element.removeAttributeNode (MDN):

JavaScript
removeAttributeNode(attributeNode)

Parameters

ParameterTypeDescription
attributeNodeAttrThe attribute node to remove from the element (MDN).

Return value

TypeDescription
AttrThe attribute node that was removed (MDN).

Exceptions

NotFoundError — Thrown when the element’s attribute list does not contain the specified attribute node (MDN).

Common patterns

JavaScript
// MDN example
const d = document.getElementById("foo");
const dLang = d.getAttributeNode("lang");
const removed = d.removeAttributeNode(dLang);
// lang is now removed

// Simple delete? Prefer removeAttribute:
// d.removeAttribute("lang");

⚡ Quick Reference

GoalCode
Remove by Attr nodeel.removeAttributeNode(attr)
Get Attr firstel.getAttributeNode("lang")
Simple remove by nameel.removeAttribute("lang")
Returnsremoved Attr
Wrong Attr nodeNotFoundError
MDN statusBaseline Widely available (since July 2015)

🔍 At a Glance

Four facts to remember about Element.removeAttributeNode().

Returns
Attr

Removed node

Baseline
widely

Since July 2015

Arg
Attr

Node object

Simple
removeAttr

Usually enough

📋 removeAttributeNode() vs removeAttribute()

removeAttributeNode(attr)removeAttribute(name)
ParameterAttr nodeAttribute name string
ReturnsRemoved Attrundefined
Inspect before removeYes (MDN use case)No
Missing / wrong nodeNotFoundErrorNo error (no-op)
Typical workflowgetAttributeNode then removeOne call by name
Best forAttr node APIsEveryday attribute delete

Examples Gallery

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

📚 Getting Started

MDN’s lang example with getAttributeNode.

Example 1 — Remove lang (MDN)

Get the lang Attr node, then remove it from the element.

JavaScript
// Given: <div id="foo" lang="en-US" />
const d = document.getElementById("foo");
const dLang = d.getAttributeNode("lang");
d.removeAttributeNode(dLang);
// lang is now removed: <div id="foo" />

console.log(d.hasAttribute("lang")); // false
Try It Yourself

How It Works

MDN: pass the Attr from getAttributeNode("lang") to removeAttributeNode. The attribute disappears from the element.

Example 2 — Returned Attr Node

The method returns the removed attribute node so you can read its properties.

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

const removed = el.removeAttributeNode(titleAttr);

console.log(removed.name);  // "title"
console.log(removed.value); // previous title text
Try It Yourself

How It Works

MDN: the return value is the Attr that was removed—useful for logging or archiving the old value.

📈 Practical Patterns

Simpler alternatives, errors, and inspect-then-remove workflows.

Example 3 — Prefer removeAttribute() When Simpler

MDN: skip the Attr node when you only need to delete by name.

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

// Attr-node API (two steps)
// const node = el.getAttributeNode("lang");
// el.removeAttributeNode(node);

// Simpler (MDN recommendation)
el.removeAttribute("lang");

console.log(el.hasAttribute("lang")); // false
Try It Yourself

How It Works

Both approaches remove the attribute. Use removeAttribute() unless you specifically need the Attr object.

Example 4 — NotFoundError for Wrong Attr

Passing an Attr that does not belong to this element throws (MDN).

JavaScript
const a = document.getElementById("a");
const b = document.getElementById("b");
const bLang = b.getAttributeNode("lang");

try {
  a.removeAttributeNode(bLang); // Attr belongs to b, not a
} catch (err) {
  console.log(err.name); // "NotFoundError"
}
Try It Yourself

How It Works

MDN: the attribute node must be in this element’s attribute list. Always get the node from the same element you call removeAttributeNode on.

Example 5 — Inspect, Then Remove

Read Attr.value before removing the node.

JavaScript
const card = document.getElementById("card");
const roleAttr = card.getAttributeNode("data-role");

if (roleAttr) {
  console.log("removing", roleAttr.name, "=", roleAttr.value);
  card.removeAttributeNode(roleAttr);
}
Try It Yourself

How It Works

This is the main reason to use removeAttributeNode over removeAttribute—you already have an Attr and need its properties before deletion.

🚀 Common Use Cases

  • Removing attributes when you already hold an Attr from getAttributeNode().
  • Archiving removed attribute name/value from the returned node.
  • DOM libraries walking element.attributes NamedNodeMap entries.
  • XML/SVG tooling (namespaced attrs work with this method per MDN).
  • Legacy attribute-node APIs paired with setAttributeNode().
  • Teaching the difference between name-based and node-based attribute removal.

🧠 How removeAttributeNode() Removes an Attr

1

Obtain Attr node

const attr = el.getAttributeNode("lang") or from attributes.

Attr
2

Call removeAttributeNode

el.removeAttributeNode(attr) — must belong to this element.

Remove
3

Return removed node

You get back the Attr that was detached (MDN).

Return
4

Attribute gone from element

Verify with hasAttribute(). Default values may be re-applied per MDN notes.

📝 Notes

  • Not Deprecated, Experimental, or Non-standard on MDN (Baseline Widely available, July 2015).
  • Prefer removeAttribute() when you do not need the Attr node (MDN).
  • Returns the removed Attr node (unlike removeAttribute).
  • Throws NotFoundError if the Attr is not on this element.
  • MDN: no separate removeAttributeNodeNS—this method handles namespaced attrs too.
  • Related: getAttributeNode(), removeAttribute(), JavaScript hub.

Browser Support

Element.removeAttributeNode() 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.removeAttributeNode()

Remove an Attr node from an element — returns the removed node.

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
removeAttributeNode() Excellent

Bottom line: Use removeAttributeNode() when you have an Attr object. For everyday deletes, removeAttribute(name) is simpler.

Conclusion

Element.removeAttributeNode() removes an Attr node from an element and returns that node—ideal when you already have the attribute object. For everyday deletes, removeAttribute() is simpler; use removeAttributeNode() when you need to inspect or archive the Attr first.

Continue with getAttributeNode(), removeAttribute(), removeAttributeNS(), shadowRoot, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Get the Attr from the same element you remove from
  • Null-check after getAttributeNode() before removing
  • Use the returned Attr to log or archive old values
  • Prefer removeAttribute() when you only know the name
  • Wrap in try/catch when Attr ownership is uncertain

❌ Don’t

  • Pass an Attr from a different element
  • Use everywhere when removeAttribute() suffices
  • Forget NotFoundError for wrong Attr nodes
  • Assume removed attrs never get default values back (MDN note)
  • Confuse this with removing the element (remove())

Key Takeaways

Knowledge Unlocked

Five things to remember about Element.removeAttributeNode()

Remove by Attr node, not by name.

5
Core concepts
📄 02

Returns

removed Attr

Result
✍️ 03

Pair

getAttributeNode

Workflow
⚠️ 04

Error

NotFoundError

Throws
05

Default

removeAttribute

Simpler

❓ Frequently Asked Questions

It removes the specified Attr node from the element and returns that removed Attr node (MDN).
No. MDN marks Element.removeAttributeNode() as Baseline Widely available (across browsers since July 2015). It is not Deprecated, Experimental, or Non-standard.
An Attr node — typically obtained first with getAttributeNode() or from element.attributes (MDN).
The Attr node that was removed from the element (MDN).
MDN: if you do not need to inspect the Attr node before removing it, removeAttribute(attrName) is simpler.
NotFoundError when the element's attribute list does not contain the specified Attr node (MDN).
Did you know?

MDN notes that after you remove an attribute, the browser may immediately replace it with a default value for that attribute on the element type. Also, removeAttributeNode() works for namespaced attributes —there is no separate removeAttributeNodeNS method.

More Element Topics

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

removeAttributeNS() →

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