.test1
,
.test2
,
.test3
- убрать цифры, пусть везде будет просто
.test
const output = document.querySelector('#classlist');
const container = document.querySelector('.wrapper');
const rowSelector = '.test';
const key = 'test';
const itemSelector = `[data-${key}]`;
container.addEventListener('click', function({ target: t }) {
if (t = t.closest(itemSelector)) {
const toRemove = Array.prototype.reduce.call(
t.closest(rowSelector).querySelectorAll(itemSelector),
(acc, n) => (n !== t && acc.push(n.dataset[key]), acc),
[]
);
this.classList.remove(...toRemove);
this.classList.toggle(t.dataset[key]);
output.textContent = this.classList;
}
});