Let’s say I have multiple buttons which each have a .btn-swatch
class. When any of them is clicked, I want to act on the one that was clicked. So far, I can just figure out how to act on a specific element from the array.
const swatchBtn = document.getElementsByClassName('btn-swatch');
swatchBtn.item(1).addEventListener("click", function (e) {
const swatchColor = e.target.getAttribute('data-color');
const selectedEl = document.querySelector('[data-selected]');
selectedEl.classList.add(swatchColor);
});