JSFiddle - React, Tailwind, and code Playground
by Medesko
HTML
<a id="button" href="#" class="active">Remove this class!</a>
CSS
a {
margin:20px;
padding:10px 15px;
background:#FFD202;
border-radius:5px;
color:#222;
display:inline-block;
text-decoration:none;
}
.active {
background:#000;
color:#FFD202;
}
JavaScript
// hasClass
function hasClass(elem, className) {
return new RegExp(' ' + className + ' ').test(' ' + elem.className + ' ');
}
// removeClass
function removeClass(elem, className) {
var newClass = ' ' + elem.className.replace( /[\t\r\n]/g, ' ') + ' ';
if (hasClass(elem, className)) {
while (newClass.indexOf(' ' + className + ' ') >= 0 ) {
newClass = newClass.replace(' ' + className + ' ', ' ');
}
elem.className = newClass.replace(/^\s+|\s+$/g, '');
}
}
document.getElementById('button').onclick = function() {
removeClass(this, 'active');
this.innerHTML = 'Yellow is much nicer.';
}