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:
- old: It is a required parameter, that specifies the name of the token that would be replaced.
- new: It is a required parameter, that specifies the name of the new token that replaces with.
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:
- Google Chrome 61 and above
- Mozilla Firefox 49 and above
- Edge 17 and above
- Safari 10.1 and above
- Opera 48 and above
- Internet Explorer not supported