JS код работает только у первого элемента списка?
Почему js работает только в первом элементе списка?
const play = document.querySelector('.podcast-cart__content-btn-play');
const pause = document.querySelector('.podcast-cart__content-btn-pause');
function clickPlay() {
play.style.color = '0';
play.style.visibility = 'hidden';
pause.style.opacity = '1';
pause.style.visibility = 'visible';
pause.style.transform = 'scale(1.1)';
};
function clickPause() {
pause.style.opacity = '0';
pause.style.visibility = 'hidden';
play.style.opacity = '1';
play.style.visibility = 'visible';
};
play.addEventListener('click', clickPlay);
pause.addEventListener('click', clickPause);
// for (var i = 0; i < play.length; i++) {
// play[i].addEventListener("click", function () {
// play.style.opacity = '0';
// play.style.visibility = 'hidden';
// pause.style.opacity = '1';
// pause.style.visibility = 'visible';
// pause.style.transform = 'scale(1.1)';
// });
// };
.podcast-cart {
display: inline-block;
border: 1px solid red;
}
.podcast-cart__content-btn-play {
width: 42px;
max-width: 100%;
height: 41px;
max-height: 100%;
background-color: blue;
border-radius: 50px;
cursor: pointer;
transform: scale(1);
transition: all 0.2s linear;
}
.podcast-cart__content-btn-pause {
width: 42px;
max-width: 100%;
height: 41px;
max-height: 100%;
background-color: green;
border-radius: 50px;
cursor: pointer;
opacity: 0;
visibility: hidden;
transition: all 0.2s linear;
}
<article class="podcast-cart">
<div class="podcast-cart__content-btn-play podcast-cart__content-btn" role="button" aria-label="Плэй">
</div>
<div class="podcast-cart__content-btn-pause podcast-cart__content-btn" role="button" aria-label="Пауза">
</div>
</article>
<article class="podcast-cart">
<div class="podcast-cart__content-btn-play podcast-cart__content-btn" role="button" aria-label="Плэй">
</div>
<div class="podcast-cart__content-btn-pause podcast-cart__content-btn" role="button" aria-label="Пауза">
</div>
</article>
<article class="podcast-cart">
<div class="podcast-cart__content-btn-play podcast-cart__content-btn" role="button" aria-label="Плэй">
</div>
<div class="podcast-cart__content-btn-pause podcast-cart__content-btn" role="button" aria-label="Пауза">
</div>
</article>
https://codepen.io/Jane111/pen/poZWaRZ
Пробовал находить две кнопки через querySelectorAll. Получал ошибку, что функция не найдена.
Далее пробовал использовать цикл (в комментариях по ссылке) Но получал ошибку в строке 5.
Почему при использовании цикла, js не меняет css свойство, а выдает ошибку?