TokenList method replace() in Javascript
The TokenList method replace() used to replace the existing token with given token in the DOMTokenList. It returns boolean value true, if the token was replaced, otherwise false.
Syntax
 1 domtokenlist.replace(<old>, <new>)
old : a old token that exists in the DOMTokenList, it is required
new : a new token that will be replaced with old token in the DOMTokenList, it is required
DOMTokenList method replace()
 1 <!DOCTYPE html>
 2 <html>
 3 <style>
 4   .apply {
 5     color: red;
 6   }
 7   .custom {
 8     color: blue;
 9   }
 10 </style>
 11 <body>
 12 	<h1>The DOMToken Object</h1>
 13 	<h2>The replace() method</h2>
 14 
 15 	<p id="content" class="apply">TokenList items</p>
 16 	<p id="result"></p>
 17 	<button onclick="replaceToken()">Replace Token apply with custom</button>
 18     
 19 	<script>
 20 	  function replaceToken() {
 21 	    const element = document.getElementById("content");
 22 	    const tokenList = element.classList;
 23 	    const resp = tokenList.replace('apply', 'custom');	
 24 	    const str = `Is token replaced ? <b>${resp}</b>`;
 25 	    document.getElementById("result").innerHTML = str;
 26 	  }
 27 	</script>
 28 </body>
 29 </html>
In the above example, a document is created with elements that include script element. A script element define a function that will be called on button click and replace the class by using replace() method. A string form with the result of replace() method that modify the content of paragraph element and display on UI.
DOMTokenList method replace()
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us