JavaScript Element setAttribute() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

Element.setAttribute() sets the value of an attribute on the element (MDN). If the attribute already exists, the value is updated; otherwise a new attribute is added. Read with getAttribute(); remove with removeAttribute().

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

Syntax

Call el.setAttribute. Start with this line.

JavaScript
el.setAttribute(name, value)
CallWhat it does
el.setAttribute(name, value)Primary signature.
el.setAttribute(...)Call with the arguments your case needs.

Built into the browser. Call setAttribute(...) on an Element — no import required.

Library declarations
JavaScript
el.setAttribute(name, value)

Parameters

ParameterDescription
qualifiedNameRequired. Attribute name (prefix:localName or localName)
valueRequired. String (or Trusted Type when enforced)

Return Value

Return ValueDescription
*Always undefined

JavaScript Examples

Example 1 — Set a Safe Attribute

MDN: set name, then read it back with getAttribute().

JavaScript
const btn = document.getElementById("hello_button");

btn.setAttribute("name", "helloButton");
console.log(btn.getAttribute("name")); // "helloButton"
Try It Yourself

How It Works

  1. Name + value. Two string arguments.
  2. Create or update. Missing → add; present → replace value.
  3. Confirm. getAttribute("name") returns the string.
  4. Safe attribute. name is not an XSS sink (MDN).

Example 2 — Toggle disabled

MDN: presence means true; remove the attribute to enable again.

JavaScript
const btn = document.getElementById("hello_button");

if (btn.hasAttribute("disabled")) {
  btn.removeAttribute("disabled");
} else {
  btn.setAttribute("disabled", "disabled");
}

console.log("disabled?", btn.hasAttribute("disabled"));
Try It Yourself

How It Works

  1. Presence = true. Value can be "" or "disabled".
  2. Off. Only removeAttribute() clears a boolean attr.
  3. Check. Prefer hasAttribute() over reading the value.
  4. Shortcut. toggleAttribute("disabled") does the same flip.

Example 3 — Set data-*

Custom data attributes work the same way with setAttribute().

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

card.setAttribute("data-id", "42");
card.setAttribute("data-role", "admin");

console.log(card.getAttribute("data-id")); // "42"
console.log(card.dataset.id);              // "42"
Try It Yourself

How It Works

  1. Full name. Use "data-id" with setAttribute.
  2. dataset. Reflects as dataset.id (camelCase).
  3. Always strings. Numbers become string attribute values.
  4. Still sanitize if you later inject those values into HTML.

Browser Support

Element.setAttribute() is Baseline Widely available (MDN, since July 2015). It is not Deprecated, Experimental, or Non-standard. Trusted Types enforcement for unsafe attributes depends on CSP / browser support.

✓ Baseline Widely available

Element.setAttribute()

Set or update any attribute by name. Prefer safe attributes; never feed untrusted strings into XSS sinks.

Baseline Widely available
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Android WebView Supported · Modern WebView
Full support
setAttribute() Baseline

Bottom line: Safe and universal for ordinary attributes. Treat event handlers, srcdoc, and script URLs as security-sensitive.

Frequently Asked Questions

It sets the value of an attribute on the element. If the attribute already exists, the value is updated; otherwise a new attribute is added (MDN).
No. MDN marks Element.setAttribute() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
undefined. There is no return value (MDN).
Set value to "" or "disabled". Presence means true; call removeAttribute() to turn it off (MDN).
Some attributes (onclick, srcdoc, script src) are XSS injection sinks (MDN). Never pass untrusted strings to those. Prefer safe attributes or Trusted Types.
IDL properties reflect specific attributes and may coerce types. setAttribute() sets any attribute by name as a string (or trusted type).

Next: Element setAttributeNode()

Attach an Attr node when you already have the attribute object.

setAttributeNode() →

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