Open In App

HTML DOM TokenList.replace() Method

The replace() method in HTML DOM is used to replace or change an old token with a new one in a DOM TokenList Object. 

Syntax:



domtokenlist.replace(old, new)

Parameter Values:  It contains two values:

Example: Below HTML code demonstrate the use of replace() method in HTML DOM. 






<!DOCTYPE html>
<html>
 
<head>
    <style>
        .geek {
            background-color: coral;
            font-size: 50px;
        }
 
        .hello {
            background-color: green;
            font-size: 30px;
            font-style: italic;
        }
    </style>
</head>
 
<body>
    <h2 id="gfg" class="geek">
        GeeksforGeeks
    </h2>
    <h2>
        HTML DOM TokenList replace() Method
    </h2>
    <button onclick="myGeek()">Submit</button>
 
    <script>
        function myGeek() {
            var elem = document.getElementById("gfg");
     
            // Replacing class from div element
            // with a new class
            elem.classList.replace("geek", "hello");
        }
    </script>
</body>
 
</html>

Output:

 

Supported Browsers: 


Article Tags :