When I click on the play icon, it changes to the pause icon. However, I am only able to change the first icon from play to pause in my JavaScript code. How can I apply this functionality to all the audio elements on the page?
let playIcon = "https://image.flaticon.com/icons/svg/149/149668.svg";
let pauseIcon = "https://image.flaticon.com/icons/svg/149/149670.svg";
function togglePlay(video) {
var audio = document.getElementsByTagName("audio")[0];
if (!audio)
return;
audio.paused ? audio.play() : audio.pause();
document.getElementById("button").src = audio.paused ? playIcon : pauseIcon;
}
#button {
width: 50px;
display: block;
}
<div class="player" onclick="togglePlay(this)">
<img src="https://image.flaticon.com/icons/svg/149/149668.svg" id="button">
<audio>
<source src="https://ccrma.stanford.edu/~jos/mp3/bachfugue.mp3" />
</audio>
</div>