HTML DOMTokenList replace()
❮ The DOMTokenList ObjectExamples
Replace a CSS class with another:
const list = element.classList;
list.replace("myStyle", "newStyle");
Try it Yourself »
Add a "myStyle" class to an element:
const list = element.classList;
list.add("myStyle");
Try it Yourself »
Remove the "myStyle" class from an element:
const list = element.classList;
list.remove("myStyle");
Try it Yourself »
Toggle "myStyle" on and off:
const list = element.classList;
list.toggle("myStyle");
Try it Yourself »
Definition and Usage
The replace()
method replaces a token in a
DOMTokenList.
See Also:
Syntax
domtokenlist.replace(old, new)
Parameters
Parameter | Description |
old | Required. The token to replace. |
new | Required. The token to replace with. |
Return Value
Type | Description |
Boolean | true if the token was replaced, otherwise false . |
Browser Support
domtokenlist.replace()
is an ECMAScript7 (ES7) feature.
ES7 (JavaScript 2016) is supported in all modern browsers:
Chrome | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes |
domtokenlist.replace()
is not supported in internet Explorer or Edge 17 (or earlier).
❮ The DOMTokenList Object