I am trying to add a mute button for this sound that plays in the background on my webpage. Can someone please help me try to do this? Here is my HTML code that plays the sound:
<audio id="sound" autoplay="autoplay" loop="loop">
<source src="song.mp3" type="audio/mp3">
</audio>
You'll have to access your audio element and trigger the muted attribute.
Example on how to do that:
<!-- HTML -->
<button onclick="toggleMuted()">Mute|Unmute</button>
<audio id="sound" autoplay="autoplay" loop="loop">
<source src="song.mp3" type="audio/mp3">
</audio>
// JS
function toggleMuted() {
var sound = document.getElementById('sound');
sound.muted = !sound.muted;
}