mute and unmute button toggle html5 audio

numerosept picture numerosept · Oct 17, 2011 · Viewed 19.4k times · Source

I've understood how to mute an <audio> sound object by putting this code :

<a href="#" onclick="document.getElementById('ambiance').muted = true; return false">
    mute sound
</a>

Now I'm searching how to mute/unmute my sound using the same button with the option to toggle it ?

Can anyone give me directions?

Answer

Esailija picture Esailija · Oct 17, 2011
var audioElm = document.getElementById('ambiance'); audioElm.muted = !audioElm.muted;