I think this example will help you <html> <head> <title>Play and stop music</title> <script type="text/javascript"> function playmusic(){ var embed = document.createElement("embed"); embed.setAttribute("id", "music"); embed.setAttribute("src", "music.mp3"); embed.setAttribute("hidden", true); embed.setAttribute("autostart", true); document.body.appendChild(embed); document.getElementById("image").setAttribute("onclick","stopmusic();"); } function stopmusic(){ document.body.removeChild(document.getElementById("music")); document.getElementById("image").setAttribute("onclick","playmusic();"); } </script> </head> <body> <img id="image" src="image.jpg" onclick="playmusic();"> </body> </html> Code (JavaScript):