I'm currently developing a straightforward add to cart container that also has the ability to toggle between different product sizes.
However, I am facing difficulties in achieving this functionality without having to create separate functions for each button.
Below is the JavaScript code snippet:
const sizeBtn = document.getElementById("sizebutton");
const sizeBtnTwo = document.getElementById("sizebutton1");
const sizeBtnThree = document.getElementById("sizebutton2");
sizeBtn.addEventListener('click', sizeBtnActive);
sizeBtnTwo.addEventListener('click', sizeBtnActive);
sizeBtnThree.addEventListener('click', sizeBtnActive);
function sizeBtnActive () {
sizeBtn.classList.toggle('active');
sizeBtnTwo.classList.toggle('active');
sizeBtnThree.classList.toggle('active');
}
Including CSS styles for better understanding:
.size-btn.faded,
.size-btn.active {
font-size: 12px;
color: #c2c2c2;
background-color: #f0f0f0;
width: 38px;
height: 42px;
border-radius: 0.5rem;
border: none;
margin-right: 10px;
margin-bottom: 35px;
cursor: none;
}
.size-btn.active {
color: #ffff;
background-color: #000000;
box-shadow: 1px 2px 10px rgba(0, 0, 0, 0.3);
transition: 1.5s ease;
cursor: pointer;
}
If you can provide any guidance or suggestions on how to improve this code, it would be highly appreciated.
I have attempted to find solutions by researching similar issues online, but none of them have been effective so far. My goal is to toggle between each button individually without activating all of them at once.