+ 1

How do I play audio onClick()

How do I play audio when an element is clicked on?

22nd Oct 2018, 5:10 AM
Noah Garza
Noah Garza - avatar
5 Respostas
+ 6
var audio = new Audio(URL-for-audio-file-here); element.onclick = function() { audio.play(); };
22nd Oct 2018, 6:00 AM
Rowsej
Rowsej - avatar
+ 4
quick easy way is to simply <audio controls src=" the audio.mp3 url here " alt="audio.mp3" ></audio>
22nd Oct 2018, 5:58 AM
BroFar
BroFar - avatar
+ 3
The url would be in quotes else it would be an error var audio = new Audio('my-audio-link.mp3') Jingga Sona
22nd Oct 2018, 6:22 AM
Lord Krishna
Lord Krishna - avatar
+ 2
oh okay!👍 thanks @lord krishna and @rowsej (signal error so i cant mention you two)
22nd Oct 2018, 6:27 AM
Jingga Sona
Jingga Sona - avatar
+ 1
Rowsej what if the link contain "http://"? the "//" will be converted to comment
22nd Oct 2018, 6:16 AM
Jingga Sona
Jingga Sona - avatar