In this post, we will show how to use audio element provided in HTML5 standard
to play sound when a HTML element is clicked. Before diving into this new
feature, please take a look at browser compatibility of HTML5 audio tag.
<!doctype html><html><head><metacharset="utf-8"><title>JavaScript Play Sound on Click Event of DOM Element</title><metaname="viewport"content="width=device-width, initial-scale=1"></head><body><buttonid="bt"type="button"onclick="javascript:PlaySound('Wat_Metta_Buddha_Qualities.mp3');">
Click Me to Play Sound
</button><scripttype="text/javascript">functionPlaySound(path){varaudioElement=document.createElement('audio');audioElement.setAttribute('src',path);audioElement.play();}</script></body></html>
The PlaySound function plays audio file of given path or URL if the button is
clicked.