In general, I have 3 blocks with buttons. When you click on the button, the picture is replaced by another one. But it is necessary to make sure that when the next track is activated, the previous button returns to its original state.
let btn = document.querySelectorAll('.btn-wrapper');
btn.forEach(function(element) {
element.addEventListener('click', function(e) {
element.querySelector('.play__btn').classList.toggle('play__btn-hidden');
element.querySelector('.pause__btn').classList.toggle('pause__btn-active');
})
})
.flex {
display: flex;
align-items: center;
}
.card__title {
margin-right: 20px;
}
.btn {
width: 17px;
height: 17px;
cursor: pointer;
}
.play__btn-hidden {
display: none;
}
.pause__btn {
display: none;
}
.pause__btn-active {
display: block;
}
<div class="cards">
<div class="card flex">
<h4 class="card__title">Track 01</h4>
<div class="btn-wrapper">
<img src="https://cdn-icons-png.flaticon.com/512/153/153752.png" alt="" class="play__btn btn">
<img src="https://cdn-icons-png.flaticon.com/512/1214/1214679.png" alt="" class="pause__btn btn">
</div>
</div>
<div class="card flex">
<h4 class="card__title">Track 02</h4>
<div class="btn-wrapper">
<img src="https://cdn-icons-png.flaticon.com/512/153/153752.png" alt="" class="play__btn btn">
<img src="https://cdn-icons-png.flaticon.com/512/1214/1214679.png" alt="" class="pause__btn btn">
</div>
</div>
<div class="card flex">
<h4 class="card__title">Track 03</h4>
<div class="btn-wrapper">
<img src="https://cdn-icons-png.flaticon.com/512/153/153752.png" alt="" class="play__btn btn">
<img src="https://cdn-icons-png.flaticon.com/512/1214/1214679.png" alt="" class="pause__btn btn">
</div>
</div>
</div>
4
Answers
You can do something like this:
Here is a slightly different way to handle it:
I simplified your code version. There is no need to switch classes on both elements if u want to change only visibility.
Just filter the buttons collection for the active and remove it.
I modified your script a bit:
What’s happening is:
I hope this helps!