HTML DOMTokenList replace() Method

Definition and Usage

The replace() method replaces the token in the DOMTokenList.

Example

Example 1

Replace with another CSS class:

const list = element.classList;
list.replace("myStyle", "newStyle");

Try It Yourself

Example 2

Add the "myStyle" class to the element:

const list = element.classList;
list.add("myStyle");

Try It Yourself

Example 3

Remove the "myStyle" class from the element:

const list = element.classList;
list.remove("myStyle");

Try It Yourself

Example 4

Toggle the open/close of "myStyle":

const list = element.classList;
list.toggle("myStyle");

Try It Yourself

Syntax

domtokenlist.replace(old, new)

Parameters

Parameters Description
old Required. The tag to be replaced with.
new Required. The tag to be replaced.

Return Value

Type Description
Boolean Value Returns true if the tag is replaced, otherwise false.

Browser Support

domtokenlist.replace() is an ECMAScript7 (ES7) feature.

All modern browsers support ES7 (JavaScript 2016):

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
Support Support Support Support Support

Internet Explorer or Edge 17 (or earlier versions) do not support domtokenlist.replace().

Related Pages

length Property

item() Method

add() Method

remove() Method

toggle() Method

forEach() Method

entries() Method

keys() Method

values() Method

DOMTokenList Object