+ 2

How to play an music on onclick button using JavaScript

14th Jun 2017, 8:25 AM
CHINTAN JAIN
CHINTAN JAIN - avatar
5 Answers
+ 12
Just for completeness, you could use the Audio object to create an audio element without HTML. var audio = new Audio( "file.mp3" ); audio.play();
14th Jun 2017, 8:47 AM
Maz
Maz - avatar
+ 9
button.onclick = function(){ audio.play(); } // button = your button // audio = your HTML audio element
14th Jun 2017, 8:41 AM
Maz
Maz - avatar
+ 9
Yes, and it is preferable use the <source> tag inside the <audio> tag with multiple extension! ;) <!-- Classic syntax --> <audio src = "file.mp3"> </audio>
14th Jun 2017, 8:44 AM
Maz
Maz - avatar
0
so I need to code of an audio file via HTML right
14th Jun 2017, 8:43 AM
CHINTAN JAIN
CHINTAN JAIN - avatar
14th Jun 2017, 8:45 AM
CHINTAN JAIN
CHINTAN JAIN - avatar