
Rattribute.js is a dependency-free JavaScript utility that changes HTML attributes at defined viewport widths.
It works with built-in xs through xxxl thresholds, custom pixel widths, multiple attribute assignments, and values returned from JavaScript functions.
Rattribute.js updates class, target, ARIA attributes, or any other HTML attribute as the viewport changes.
It also watches for new DOM elements by default and exposes methods for pausing updates, rescanning the document, refreshing values, and changing runtime configuration.
Features
- Built-in viewport thresholds from
xsthroughxxxl. - Custom numeric breakpoints for project-specific widths.
- Multiple attribute assignments in one
data-*rule. - Attribute values returned from JavaScript functions.
- Original attribute values restored outside active breakpoints.
- Automatic discovery of DOM elements inserted after page load.
- Per-element ignore control.
- Runtime controls for scanning, refresh, automation, and configuration.
How To Use It
Installation
Load the minified build directly in your document:
<script src="https://cdn.jsdelivr.net/gh/williamtroup/Rattribute.js/dist/rattribute.min.js"></script>
Or install the package with NPM:
npm install rattribute.js
Basic Usage
Rattribute.js starts automatically after the DOM is ready. No initialization call is required for the default setup.
A responsive rule uses attribute=value;attribute=value inside a breakpoint data attribute. This link keeps its original attributes below 992px. At 992px and above, Rattribute.js changes both target and class.
<a href="https://example.com/docs" class="docs-link" target="_self" rel="noopener" data-rattribute-js-lg="target=_blank;class=docs-link docs-link--desktop"> Documentation </a>
Built-In Breakpoints
| Data Attribute | Applies When |
|---|---|
data-rattribute-js-xs | Below 576px |
data-rattribute-js-sm | 576px and above |
data-rattribute-js-md | 768px and above |
data-rattribute-js-lg | 992px and above |
data-rattribute-js-xl | 1200px and above |
data-rattribute-js-xxl | 1400px and above |
data-rattribute-js-xxxl | 1600px and above |
Custom Breakpoints
Use a numeric suffix for a custom minimum width. This rule applies from 850px.
<button type="button" class="menu-toggle" aria-label="Open menu" data-rattribute-js-850="class=menu-toggle menu-toggle--wide;aria-label=Open desktop menu"> Menu </button>
Use Function Results As Attribute Values
Rattribute.js resolves function references from globalThis. The function result becomes the new attribute value.
<script>
function getNavigationLabel() {
return 'Open desktop navigation';
}
</script>
<button
type="button"
aria-label="Open navigation"
data-rattribute-js-lg="aria-label=getNavigationLabel()">
Menu
</button>
Function arguments are split on commas, and each argument is parsed as JSON. Quoted strings, numbers, booleans, and other simple JSON values work well here.
<script>
function buildLabel(section, count) {
return `${section} navigation, ${count} items`;
}
</script>
<button
type="button"
aria-label="Navigation"
data-rattribute-js-lg='aria-label=buildLabel("Primary", 4)'>
Menu
</button>
Ignore Elements
Set data-rattribute-js-ignore="true" when an element should not be processed.
<div data-rattribute-js-ignore="true" data-rattribute-js-lg="class=desktop-panel"> ... </div>
Configuration Options
responsiveDelay(number, default250): Delay in milliseconds before responsive rules run after a resize event.removeAttributes(boolean, defaulttrue): Removes Rattribute.js data attributes after they have been processed.enabled(boolean, defaulttrue): Starts responsive automation when the library loads.observationMode(boolean, defaulttrue): Watches the DOM for newly inserted elements and processes them automatically.assignMissingIds(boolean, defaultfalse): Generates an ID for processed elements that do not already have one. The current implementation usescrypto.randomUUID(), which requires a secure context in supporting browsers.elementIdPrefix(string, default"rattribute"): Prefix used for generated IDs whenassignMissingIdsis enabled.
$rattribute.setConfiguration({
responsiveDelay: 150,
removeAttributes: false
});
API Methods
| Method | Returns | Purpose |
|---|---|---|
start() | $rattribute | Starts responsive updates. |
stop() | $rattribute | Stops responsive updates. |
fetch() | $rattribute | Scans the document for responsive elements. |
refresh() | $rattribute | Reapplies responsive values to registered elements when automation is enabled. |
getElements() | HTMLElement[] | Returns the registered responsive elements. |
getIgnoredElements() | HTMLElement[] | Returns elements currently marked to be ignored. |
enableIgnoredElements() | $rattribute | Removes the ignore state, processes those elements, and clears the ignored-elements list. |
setConfiguration(configurationOptions) | $rattribute | Updates recognized configuration values. |
getVersion() | string | Returns the library version. |
Alternatives And Related Resources
- Responsive Link Targets by Viewport Width – Rink.js
- Responsive Breakpoint Management with JsWidthBreakpoints Library






