DOM Methods

DOMTokenList.replace()

Replaces a present token and returns true. If the old token is absent, returns false without adding a replacement. Use the boolean result to distinguish a completed state change from an absent old token. If the new token already exists, the operation keeps the set unique. If either token is invalid, the method throws. Example result: true:card ready.

Formal Syntax & Structure

html syntaxhtml
list.replace(oldToken, newToken)

Parameters & Attributes

NameTypeDescription
oldTokenstringToken to replace.
newTokenstringReplacement token.

Return Value

boolean

True if the old token was replaced; false if it was absent.

Working Example

Example Snippethtml
<p>  Result:  <output id="result"></output></p><script>  const result = document.getElementById("result");  (async () => {  const el = document.createElement("div"); el.className = "card active"; const list = el.classList;  result.textContent = list.replace("active","ready") + ":" + el.className;  })().catch(error => { result.textContent = "Error: " + error.name; });</script>
HTML Reference