replace() method of the
Let us say you have the following HTML element:
<div class="pizza spicy hot crispy">🍕</div>
To replace the
spicy class with the
olive class, you can do the following:
const div = document.querySelector('div') div.classList.replace('spicy', 'olive')
replace() method returns
true if the class is successfully replaced with the new class. Otherwise,
false is returned.
Unlike add(), remove(), and toggle() methods of the
classList property, the
replace() method doesn't work in IE. You can only use it in modern browsers.
✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.