JSFiddle - React, Tailwind, and code Playground
HTML
<div class="buttons">
<div class="btn btn-reset" data-class="reset"></div>
<div class="btn btn-red" data-class="red"></div>
<div class="btn btn-green" data-class="green"></div>
<div class="btn btn-yellow" data-class="yellow"></div>
</div>
<div class="container custom-class-1 custom-class-2">
When I click on the colored buttons, then container color class will added/exchanged. When I click on the white button, then it removes container color class. <strong>How to do it?</strong>
</div>
CSS
/* Buttons */
.btn {
display: inline-block;
width: 30px;
height: 30px;
margin: 3px;
cursor: pointer;
border: 2px solid #333;
}
.btn.btn-red { background-color: red; }
.btn.btn-green { background-color: green; }
.btn.btn-yellow { background-color: yellow; }
/* Container */
.container {
max-width: 480px;
margin-top: 20px;
padding: 40px;
border: 2px solid #DDD;
}
/* Container color classes */
.container.cont-red { background-color: red; }
.container.cont-green { background-color: green; }
.container.cont-yellow { background-color: yellow; }
/* Container custom classes */
.container.custom-class-1 { border-width: 6px; }
.container.custom-class-2 { border-color: #333; }
JavaScript
$('.btn').click(function()
{
var color = $(this).attr('data-class');
if ($(this).hasClass('btn-reset')) {
$('.container').attr('class', 'container')
} else {
$('.container').attr('class', 'container cont-'+ color)
}
})