const blockSelector = '.block';
const buttonSelector = '[data-class]';
const activeClass = 'active';
const blocks = document.querySelectorAll(blockSelector);
const buttons = [...document.querySelectorAll(buttonSelector)];
const classes = buttons.map(n => n.dataset.class);
document.body.addEventListener('click', e => {
const button = e.target.closest(buttonSelector);
if (button) {
blocks.forEach(n => {
n.classList.remove(...classes);
n.classList.add(button.dataset.class);
});
buttons.forEach(n => n.classList.toggle(activeClass, n === button));
}
});