adding sound effect when a mouse button is clicked

mohanad-raheem

Jan 13th, 2020 06:56 PM

hi total beginner here what is the code for playing a sound effect when a mouse button is clicked i mean even when i click on empty space with no buttons or any elemnt,and where should the code go head body? thanks

bobbyiliev

Sep 27th, 2022 07:38 AM

Hi there,

I am just following up on some old unanswered questions on the site.

Here is a quick snippet to do that:

<!doctype html>
<html>
  <head>
    <title>Audio</title>
  </head>
  <body>

    <script>
      function play() {
        var audio = document.getElementById("audio");
        audio.play();
      }
    </script>

    <input type="number" min="10" max="100" onclick="play()">
    <audio id="audio" src="https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.mp3"></audio>

  </body>
</html

Best,

Bobby