Here's roughly what my script looks like.
var ButtonSelect = document.querySelectorAll(".musicBtn")
ButtonSelect.forEach(element => element.addEventListener("click", playMusic))
function playMusic(){
var audio = new Audio("music1.mp3");
if (this.id == "A")
audio.play();
else if (this.id == "B") {
audio.src="music2.mp3";
audio.play();
} else if (this.id == "C") {
audio.src="music3.mp3";
audio.play();
} ...
}
And it pretty much goes on like this. So, depending on which button is pressed, a different song plays. But I want to make it so that not only does pressing a button start the corresponding song, but it must also stop whatever song is currently playing. Since the audio object is created locally and disappears after the function call, how do I go about that? Isn't there any way to just ask the page to stop whatever sound is playing without specifying where it comes from?