![]() ![]() If programmers do not like to use the onClick attribute of the HTML to run a JavaScript function, then they can use JavaScript's Event Listener to play the specific audio file on a button click. It is an alternative way to play an audio file in HTML using JavaScript. Method 2: Using the JavaScript Click Event and HTML to play the audio: play() method will play the desired elements file. ![]() The JS document.getElementById() will set the Audio Tag's property, i.e., the audio file and the JavaScript. play() Methods to use the properties added inside the onClick attribute of the tag. We used the JavaScript document.getElementById() and. But before that, we need to add the required JS code inside that Function to play the audio file. ![]() When we call this function, it will run the HTML onClick attribute.Īgain, when we call the onClick attribute, we can run the JavaScript function. We need to create a function named MyAudio(). Then, we used the onClick Attribute of HTML to Play HTML Audio using JavaScript and HTML. Here in the code, we have used the HTML tag with the src attribute to add our local audio file. For example,
0 Comments
Leave a Reply. |