I am trying to toggle 'disable' for 'active' CSS class to make the fill property of some SVG change when it is clicked.
I am able to make the first element change correctly but when trying the same with the second and third SVGs it changes the color of the first one inside the first div.
HTML
<div>
<svg onclick="toggleColor()" class="home__like disable heart">
<use xlink:href="img/sprite.svg#icon-heart-full"></use>
</svg>
</div>
<div>
<svg onclick="toggleColor()" class="home__like disable heart">
<use xlink:href="img/sprite.svg#icon-heart-full"></use>
</svg>
</div>
<div>
<svg onclick="toggleColor()" class="home__like disable heart">
<use xlink:href="img/sprite.svg#icon-heart-full"></use>
</svg>
</div>
CSS
.disable {
fill: #fff;
}
.active {
fill: $color-primary;
}
JavaScript
function toggleColor() {
const toggleHeart = document.querySelector('.heart');
if(toggleHeart.classList.contains('disable')) {
toggleHeart.classList.remove('disable');
toggleHeart.classList.add('active');
} else {
toggleHeart.classList.remove('active');
toggleHeart.classList.add('disable');
}
}