Try the below code snippet

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

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

    <input type="button" value="PLAY" onclick="play()">
    <audio id="audio" src="https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.mp3"></audio>

  </body>
</html>

Answer from Arunkumar Vasudevan on Stack Overflow
Discussions

Play sound on click, then go to page
Hello! I am making an quiz in HTML with questions and 4 answers by each question. By clicking on correct answer, I want to play a short sound, then go to next html page. How can I do it to play sound on click? Please help! More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
0
May 20, 2021
html - How can I play a random sound on click in a web page? - Stack Overflow
I want a random sound to play on a click on a button on a web page. I have researched it quite a bit and this discussion has helped me most: http://www.elated.com/forums/topic/5196/ One poster More on stackoverflow.com
🌐 stackoverflow.com
Playing sound on a button click
I am new to javascript and want to make sure I am on the right track when playing a sound. Is there anything I should not be doing better or not be doing at all. Below is a simple function. More on codereview.stackexchange.com
🌐 codereview.stackexchange.com
February 12, 2013
How to play an audio when clicking on a link, or just clicking in general?
So you can do this quickly by adding an audio element and playing it through JavaScript: play More on reddit.com
🌐 r/HTML
2
3
July 11, 2024
🌐
Reddit
reddit.com › r/html › how to play an audio when clicking on a link, or just clicking in general?
r/HTML on Reddit: How to play an audio when clicking on a link, or just clicking in general?
July 11, 2024 -

I apologize for the lack of any code here, I am extremely new to HTML and am trying to create a personal website. I want to play a silly sound effect for when you click on anything on this page, but i can't seem to find suitable code, or just code that makes any sense to me.

I want the audio to be able to replay, not take you to the source of the audio, y know, just a simple sound effect.

🌐
Allwebco-templates
allwebco-templates.com › support › S_audio_onmouseover.htm
Simple audio on click, HTML5 sound when clicking
You should hear a short click when you click on any of these 3 elements. [ view example ] Click For Sound · Click For Sound Adding click sound to your webpage: Uses the HTML5 "audio" tag. Follow the steps below to add sound to any HTML webpage. Step 1: Download the click sound files Step 2: ...
🌐
ThoughtCo
thoughtco.com › play-sound-on-click-or-rollover-3469484
How Do You Make a Sound Play on Click or Rollover?
March 12, 2020 - <a href="#" onclick="playSound('UrlToSoundFile');">Click here to hear a sound</a> <p onmouseover="playSound('UrlToSoundFile');">Mouse over this text to hear a sound</p> Here is the entire HTML document, playing the sound of a bluejay.
🌐
DEV Community
dev.to › shantanu_jana › how-to-play-sound-on-button-click-in-javascript-3m48
How to Play Sound on Button Click in JavaScript - DEV Community
February 6, 2025 - This type of play sound on click you can use it everywhere in the webpage or in a certain element. In most cases we hear a beep sound in the button. So Beep Sound has been used here too. But you can use any other sound if you want. Watch its live demo to learn how it works. ... Very little JavaScript has been used to create this Play Sound Button. If you know a little JavaScript you can easily create it. HTML Code: The button was created by the following html code.
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Play sound on click, then go to page - JavaScript
May 20, 2021 - Hello! I am making an quiz in HTML with questions and 4 answers by each question. By clicking on correct answer, I want to play a short sound, then go to next html page. How can I do it to play sound on click? Please he…
Find elsewhere
🌐
W3Schools
w3schools.com › jsref › met_audio_play.asp
HTML DOM Audio play() Method
Tip: Use the controls property to display audio controls (like play, pause, seeking, volume, etc, attached on the audio). ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
Kahimyang
kahimyang.com › kauswagan › code-blogs › 1652 › how-to-play-a-sound-when-an-element-is-clicked-in-html-page
How to play a sound when an element is clicked in HTML page
April 20, 2025 - In this example we will demonstrate how to play a "click" sound when a link or other element is clicked in HTML pages. A sample code for jQueryMobile is also provided. An MP3 audio file is used play the actual sound. You have to have jQuery in your page for this to work.
🌐
Medium
mcasimirian.medium.com › playing-audio-on-click-in-your-javascript-app-72aa955068fc
Playing Audio on Click in Your JavaScript App | by Manon Sainton | Medium
May 12, 2021 - In the index.html file, there is a script tag set to JavaScript so that my .js file could be read, interpreted, and displayed to the DOM. Next, we need to find some sound clips! FreeSound.org is a great site that has a variety of cool clips. You may have to create a free account before having access to sounds. Choose something that doesn’t have a delay at the start and is decent quality. For this small project, I chose two sounds: one clip that says “Play” and another that says “Stop”. Now let’s get some code into the editor!
🌐
Foolishdeveloper
foolishdeveloper.com › how-to-play-sound-on-click-using-javascript
How to Play Sound On Click Using JavaScript
February 10, 2024 - “Play Sound onClick” in JavaScript refers to the ability to play an audio file when a button or other element is clicked. This can be accomplished using the HTML5 audio element and JavaScript play() method.
🌐
Programming Head
programminghead.com › how-to-play-audio-in-html-using-javascript
How to Play Audio in HTML using JavaScript | Audio Player
<html> <body> <audio ... } </script> </body> </html> ... To play sound when you click the HTML image, then you can simply use JavaScript onClick event in your Image and then by using that Image, you can play the Audio file....
🌐
DEV Community
dev.to › cwrcode › how-to-play-sound-on-button-click-using-javascript-3bn3
How to Play Sound On Button Click Using JavaScript? - DEV Community
May 24, 2023 - This Play Sound Button was created with only a few lines of JavaScript. It's simple to make if you know a little JavaScript. This project is intended for beginners. This project will teach you how to add a click sound to any button or element.
🌐
JavaScript Kit
javascriptkit.com › script › script2 › soundlink.shtml
Cut & Paste HTML5 Mouseover/ Click sound effect
Click here for a script that allows you to play a sound when the mouse moves over or clicks a link!
🌐
CodePen
codepen.io › Sluggern › pen › qyXgVe
Play sound on click (Vanilla JS)
let sound = document.getElementById('beep') let play = document.getElementById('play') play.onclick = function() { console.log('play'); sound.play(); return false; }; ! 999px · Clear · Ctrl Ctrl Space Autocomplete · F Find · G Find Next ...
🌐
YouTube
youtube.com › programmingtt
Button Click Sound Effect | In HTML CSS and JavaScript | HTML Tutorial For Beginners - YouTube
Button click sound effect in html css and javascript.Click.mp3 : https://www.fesliyanstudios.com/play-mp3/387Related Keywords * button click sound effect usi...
Published   June 18, 2021
Views   67K
🌐
W3Schools
w3schools.com › jsref › tryit.asp
Click the buttons to play or pause the audio.
The W3Schools online code editor allows you to edit code and view the result in your browser
🌐
YouTube
youtube.com › watch
Add Button Click Sound Effects To Your Site With Javascript - YouTube
In this tutorial, we will learn how to enhance your website by adding button click sound effects using JavaScript. Sound effects can bring interactivity and ...
Published   June 11, 2021