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
🌐
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 - To make the sound play, we need an event listener! You can use an onclick method, but in this case I’ll use addEventListener(). Within addEventListener, I’m going to give it two parameters: a “click” and an arrow function with an event passed in.
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
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
playing sound on click event with pure javascript - Stack Overflow
In my case, when i click it just sets on loop, till i refresh the page and then it stops. I want it to play only once and then stop. ... You are getting the wrong element with document.getElementById. The id of your audio tag is sounds, but you are trying to get it with the id A. More on stackoverflow.com
🌐 stackoverflow.com
July 28, 2018
javascript - Playing sound on a button click - Code Review Stack Exchange
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
🌐
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.
🌐
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
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Play sound on click, then go to page - JavaScript - The freeCodeCamp Forum
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…
🌐
Foolishdeveloper
foolishdeveloper.com › how-to-play-sound-on-click-using-javascript
How to Play Sound On Click Using JavaScript
February 10, 2024 - Play sound on click using javascript is a function that create a sound on clicking on the button as the user clicks on the button the click evenlistener triggers the sound click function and using the .play() function the sound that is stored ...
🌐
DaniWeb
daniweb.com › programming › web-development › threads › 286059 › play-sound-when-a-link-is-clicked
javascript - play sound when a link is clicked | DaniWeb
My problem is that I want the button to make a sound when it is clicked on. I figured that Javascript would be the best language to use since I know nothing about Flash. Any help here? Thanks. <html> <head> <title>Testing buttons</title> <script language="javascript" type="text/javascript"> function playSound(soundfile) { document.getElementById("dummy").innerHTML=document.getElementById("dummy").innerHTML + "<embed src=\""+soundfile+"\" hidden=\"true\" autostart=\"true\" loop=\"false\" />"; } </script> </head> <body> <a href="link.html" id="dummy"> <img src="enter.jpg" onclick="playSound('boom.wav');"> </a> </body> </html>
Find elsewhere
🌐
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.

🌐
CodePen
codepen.io › Sluggern › pen › qyXgVe
Play sound on click (Vanilla JS)
Minimize JavaScript Editor · Fold All · Unfold All · let sound = document.getElementById('beep') let play = document.getElementById('play') play.onclick = function() { console.log('play'); sound.play(); return false; }; ! 999px · Clear · ...
🌐
Allwebco-templates
allwebco-templates.com › support › S_audio_onmouseover.htm
Simple audio on click, HTML5 sound when clicking
Code for text click sound: <a href="javascript:playclip();" style="font-size: 1.5em;">Click For Sound</a><br> Code for an image click sound: <a href="javascript:playclip();"><img src="picts/sound-image.jpg" width="150" height="100" border="0" alt="Click for Sound"></a><br> Code for a div sound area: <div OnClick="playclip();" style="border: #C0C0C0 1px solid; background-color: #F0F0F0; width: 200px; padding: 8px; text-align: center; margin: 0 auto; cursor: pointer;">Click For Sound</div> Step 6 Testing: Test your page in some different browsers like IE, Chrome, Firefox and Safari if possible.
🌐
ThoughtCo
thoughtco.com › play-sound-on-click-or-rollover-3469484
How Do You Make a Sound Play on Click or Rollover?
March 12, 2020 - Create a script that adds sound effects when a customer clicks something using the attribute and when a customer rolls over something using the attribute. Test these effects in different browsers, as not all web browsers handle on mouseover and on click attributes on elements other than links. Place the following script in the head of your HTML document: <script language="javascript" type="text/javascript"> function playSound(soundfile) { document.getElementById("dummy").innerHTML= "<embed src=\"" +soundfile+"\" hidden=\"true\" autostart=\"true\" loop=\"false\" />"; } </script>
🌐
Programming Head
programminghead.com › how-to-play-audio-in-html-using-javascript
How to Play Audio in HTML using JavaScript | Audio Player
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.
🌐
W3Frontend
w3frontend.com › home › vanilla js › javascript play sound onclick event
JavaScript Play Sound onclick Event - W3Frontend
November 23, 2025 - let input_field = document.querySelector(".input_container"); let addedInput = ""; const noteC = new Audio("https://archive.org/download/24-piano-keys/key19.mp3"); const noteG = new Audio("https://archive.org/download/24-piano-keys/key23.mp3"); const noteA = new Audio("https://archive.org/download/24-piano-keys/key24.mp3"); const noteF = new Audio("https://archive.org/download/24-piano-keys/key22.mp3"); const noteE = new Audio("https://archive.org/download/24-piano-keys/key21.mp3"); const noteD = new Audio("https://archive.org/download/24-piano-keys/key20.mp3"); const keys = document.querySele
🌐
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 - 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. I hope you must have got an idea about the project. Let's have a look at our Play Sound On Click.
Top answer
1 of 4
12

There is no need to include the audio tags. Instead you can just say in javascript what audio you want to play.

function myPlay(){
    var audio = new Audio("sample.mp3");
    audio.play();
}

When you execute this function the audio will play.

2 of 4
1

Try this example, https://jsfiddle.net/nerdvoso/46f7rxbs/1/

Following code:

var playBtn = document.getElementById('play');
var stopBtn = document.getElementById('stop');

var playSound = function() {
    audio.play();
};

playBtn.addEventListener('click', playSound, false);
stopBtn.addEventListener('click', function(){audio.pause()}, false);
<audio id="audio" src="https://freewavesamples.com/files/Roland-JV-2080-101-Bass-C2.wav" preload="auto"></audio>
<button id="play">Play</button>
<button id="stop">Stop</button>

Update, https://jsfiddle.net/nerdvoso/46f7rxbs/33/

var playBtn = document.getElementById('play');
var stopBtn = document.getElementById('stop');
var nextBtn = document.getElementById('next');
var prevBtn = document.getElementById('prev');

var soundSelected = document.getElementById("audio1");

var playSound = function() {soundSelected.play();};

var stopSound = function() {soundSelected.pause();};

var nextSound = function() {
  if(soundSelected.nextElementSibling){
    soundSelected.pause();
    soundSelected.currentTime = 0;
    soundSelected = soundSelected.nextElementSibling;
  }
};


var prevSound = function() {
  if(soundSelected.previousElementSibling){
    soundSelected.pause();
    soundSelected.currentTime = 0;
    soundSelected = soundSelected.previousElementSibling;
  }
};

playBtn.addEventListener('click', playSound, false);
stopBtn.addEventListener('click', stopSound, false);
nextBtn.addEventListener('click', nextSound, false);
prevBtn.addEventListener('click', prevSound, false);
<div id="playList">
    <audio id="audio1" src="https://freewavesamples.com/files/Roland-JV-2080-101-Bass-C2.wav" preload="auto"></audio>
    <audio id="audio2" src="https://freewavesamples.com/files/Yamaha-V50-Metalimba-C2.wav" preload="auto"></audio>
</div>
<button id="prev">Prev</button>
<button id="play">Play</button>
<button id="stop">Stop</button>
<button id="next">Next</button>

Play sound by dedicated buttons, enter link description here

var playSoundBtn = document.getElementsByClassName("playSound");

var playSound = function() {
    var attribute = this.getAttribute("data-sound");
    var sounds = document.getElementsByTagName('audio');
    for(i=0; i<sounds.length; i++){
        sounds[i].pause();
        sounds[i].currentTime = 0;
    }
    document.getElementById(attribute).play();
};

for (var i = 0; i < playSoundBtn.length; i++) {
    playSoundBtn[i].addEventListener('click', playSound, false);
}
<audio id="audio1" src="https://freewavesamples.com/files/Roland-JV-2080-101-Bass-C2.wav" preload="auto"></audio>
<audio id="audio2" src="https://freewavesamples.com/files/Yamaha-V50-Metalimba-C2.wav" preload="auto"></audio>
<hr>
<button class="playSound" data-sound="audio1">Sound 1</button>
<button class="playSound" data-sound="audio2">Sound 2</button>

🌐
Medium
medium.com › @bridgettwheeler › how-to-play-an-audio-file-using-a-javascript-click-event-108467823d1
How to play an audio file inside a JavaScript click event. | by Bridgett Wheeler | Medium
August 20, 2021 - imageArea.addEventListener(‘click’, (evt) => { ... From what I read it seemed like this JavaScript code should have worked without any additional code in my HTML… However, this code did not work for me. I kept researching and tried some other things, but what eventually ended up working was this: ... If you are trying to play audio on a click event and nothing you try is working — give this a try!
🌐
CodeHim
codehim.com › home › vanilla javascript › javascript play sound onclick
JavaScript Play Sound Onclick — CodeHim
January 22, 2024 - This Vanilla JavaScript code is a web-based application to play sound effects onclick event. It works by associating specific sound notes with each button and playing the corresponding note when a button is clicked.
🌐
CodeWithRandom
codewithrandom.com › 2022 › 09 › 27 › play-sound-on-click-javascript
How to Play Sound On Button Click Using JavaScript?
March 12, 2023 - ... We will add an event listener to the button to use the javascript function to produce music when the button is clicked. The function will launch when the user presses the button, and we’ll use the audio’s play() method to start the sound.