W3cubDocs

/Web APIs

Sanitizer: setDataAttributes() method

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

The setDataAttributes() method of the Sanitizer interface sets whether data attributes will be allowed or removed by the sanitizer.

The method sets the dataAttributes property in this sanitizer's configuration.

Syntax

setDataAttributes(allow);

Parameters

allow

true if data attributes are allowed, and false if they are to be removed.

Return value

None (undefined).

Examples

>

How to sanitize data attributes

The code below shows the basic usage of the setDataAttributes() method.

// Create sanitizer (in this case the default)
const sanitizer = new Sanitizer();

// Allow data attributes
sanitizer.setDataAttributes(true);

// Remove data attributes
sanitizer.setDataAttributes(false);

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Opera Safari Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet WebView Android WebView on iOS
setDataAttributes No No 138 No No No No No No No No No

© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Sanitizer/setDataAttributes