JavaScript Element role Property

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

What You’ll Learn

Element.role is an instance property that reflects the explicit ARIA role attribute on an element. Learn what it returns, how it differs from implicit roles, when it gives null, and how to update it safely with five examples and try-it labs.

01

Kind

Instance property

02

Access

Read and set

03

Type

string or null

04

Reflects

HTML role attribute

05

Status

Baseline widely

06

Focus

Accessibility semantics

Introduction

HTML elements already come with built-in meaning. For example, a native <button> is announced like a button by assistive technologies. That built-in meaning is called an implicit role.

The role attribute lets you set an explicit ARIA role when needed, and Element.role is the JavaScript property that reflects that attribute.

JavaScript
const box = document.getElementById("card");
console.log(box.role);   // "button" or null
box.role = "button";
💡
Beginner tip

element.role only tells you the explicitly set value. It does not calculate or return the element’s implicit role for you.

Understanding the Property

MDN: the role property of the Element interface returns the explicitly set WAI-ARIA role for the element.

  • Reflects the attribute — it matches the HTML role attribute.
  • String or null — returns a role name, or null if not explicitly set.
  • Explicit only — does not expose implicit semantics unless you assign them yourself.
  • Accessibility-focused — mainly used when inspecting or updating ARIA semantics.

📝 Syntax

JavaScript
element.role
element.role = "button"

Value

A string containing the explicit role value, or null when no role attribute is present.

ItemDetail
Typestring or null
AccessReadable and writable
ReflectsHTML role attribute
Important noteDoes not return implicit roles
⚠️
Use roles carefully

Adding the wrong ARIA role can confuse screen readers. Prefer native HTML elements first, and use explicit roles only when they truly improve semantics.

📋 MDN Accessibility Example Shape

MDN demonstrates setting a presentational role on images that have no alternate text:

JavaScript
const images = document.querySelectorAll("img");
images.forEach((image) => {
  if (!image.alt) {
    image.role = "presentation";
  }
});

Related learning: ariaLabel, ariaHidden, and role in HTML accessibility patterns.

⚡ Quick Reference

GoalCode / note
Read explicit roleel.role
Set roleel.role = "button"
Remove roleel.removeAttribute("role")
Check missing roleel.role === null
Read attribute directlyel.getAttribute("role")
ImportantImplicit role is not returned automatically

🔍 At a Glance

Four facts about Element.role.

Kind
reflects

attribute

Type
string|null

explicit role

Reads
ARIA

semantics

Baseline
widely

Oct 2023+

Examples Gallery

Examples follow MDN Element: role. They focus on reading explicit roles, comparing missing values, and updating semantics clearly.

📚 Getting Started

Start by reading an explicit role from an element.

Example 1 — Read an Explicit Role

Read the value that was set with the HTML role attribute.

JavaScript
const box = document.getElementById("card");
console.log(box.role); // "button"
Try It Yourself

How It Works

The property reflects the explicit attribute value, so JavaScript reads the same role string you placed in the markup.

📈 Missing, Setting & Reflection

See how null works, how assignment updates the attribute, and when role helps accessibility cleanup.

Example 2 — Missing Role Returns null

If an element has no explicit role, the property returns null.

JavaScript
const button = document.querySelector("button");
console.log(button.role); // null
Try It Yourself

How It Works

A native button already has an implicit role, but element.role only reports explicit values, so it returns null.

Example 3 — Set a Role with JavaScript

Assign a new role and inspect the updated value.

JavaScript
const panel = document.getElementById("panel");
panel.role = "status";
console.log(panel.role);
Try It Yourself

How It Works

Writing to element.role updates the underlying role attribute on the element.

Example 4 — Property and Attribute Stay in Sync

Compare element.role and getAttribute("role").

JavaScript
const item = document.getElementById("item");
item.role = "listitem";
console.log(item.role);
console.log(item.getAttribute("role"));
Try It Yourself

How It Works

The property is a reflection of the attribute, so both values stay aligned.

Example 5 — Add Presentation Role to Decorative Images

Follow the MDN accessibility pattern for images that have no alternate text.

JavaScript
const images = document.querySelectorAll("img");
images.forEach((image) => {
  if (!image.alt) {
    image.role = "presentation";
  }
});
Try It Yourself

How It Works

This pattern marks decorative images as presentational when they have no useful text alternative.

🚀 Common Use Cases

  • Checking whether a custom widget has an explicit ARIA role.
  • Assigning roles to non-native interactive patterns when necessary.
  • Auditing markup to find elements with missing or unexpected explicit roles.
  • Marking decorative content as presentation in accessibility cleanup code.
  • Comparing JavaScript property reflection with the raw HTML attribute.

🔧 How It Works

1

Browser checks the role attribute

The property is tied to the element’s explicit ARIA role markup.

HTML
2

Reading returns a string or null

Null means there is no explicit role attribute set.

Read
3

Writing updates the attribute

Assigning a role string changes the DOM attribute value too.

Write
4

Assistive tools can use the explicit role

That explicit semantic hint is what accessibility tools can read and announce.

📝 Notes

  • Not Deprecated, Experimental, or Non-standard on MDN (Baseline Widely available).
  • element.role reflects only explicit roles, not implicit native semantics.
  • Use roles to improve accessibility, not to replace good semantic HTML without a reason.
  • Related: ariaLabel, ariaHidden, part, JavaScript hub.

Browser Support

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

Baseline Widely available

Element.role

Reflects the explicit ARIA role attribute as a string or null.

Baseline Widely available
Google Chrome Supported
Yes
Microsoft Edge Supported
Yes
Mozilla Firefox Supported
Yes
Apple Safari Supported
Yes
Opera Supported
Yes
Internet Explorer Not supported
No
role Baseline

Bottom line: Use Element.role to inspect or update explicit ARIA semantics. Remember that implicit native roles are not returned unless you set the role attribute yourself.

Conclusion

Element.role is a simple but important accessibility property. It reflects the explicit role attribute, returns a string or null, and helps you inspect or update ARIA semantics from JavaScript.

Continue with previousElementSibling, part, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Prefer semantic native HTML elements first
  • Use element.role to inspect explicit accessibility semantics
  • Check for null when no role attribute is present
  • Keep property and attribute reflection in mind while debugging
  • Use ARIA roles only when they improve the experience

❌ Don’t

  • Assume element.role returns the implicit native role
  • Add explicit roles to every element without a reason
  • Override strong native semantics carelessly
  • Use ARIA as a replacement for proper HTML structure
  • Forget to test accessibility changes with real assistive tooling

Key Takeaways

Knowledge Unlocked

Five things to remember about role

Reflects explicit ARIA semantics only, not the implicit native role.

5
Core concepts
📝02

string|null

explicit value

Type
🔍03

No implicit role

returned automatically

Rule
04

Baseline

widely available

Status
🎯05

Use carefully

for accessibility

Tip

❓ Frequently Asked Questions

It gets or sets the explicitly assigned WAI-ARIA role for an element by reflecting the HTML role attribute.
No. It returns only the explicitly set role attribute value. If an element has only an implicit role, element.role is null unless you set a role attribute yourself.
It returns null when the element does not have an explicit role attribute.
No. MDN marks Element.role as Baseline Widely available, available across browsers since October 2023.
Yes. You can assign a string such as element.role = "button" or element.role = "presentation" to update the role attribute.
No. Use explicit roles only when needed. Native HTML semantics are usually better, so avoid overriding them unless you have a clear accessibility reason.
Did you know?

A plain <button> already behaves like a button for assistive technologies, but button.role still returns null unless you explicitly set a role attribute.

Previous: previousElementSibling

Learn how Element.previousElementSibling finds the element just before the current one.

← previousElementSibling

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.

5 people found this page helpful