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
🌐
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: ...
Discussions

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
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
🌐
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.

🌐
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.
🌐
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.
🌐
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.
🌐
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.
🌐
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!
🌐
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!
🌐
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.
🌐
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....
🌐
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
Top answer
1 of 2
5

Without JavaScript? I don't think so but the JavaScript is very easy.

You can play an audio element that you have put in your page withdocument.getElementById('audioTag').play(); So this should work:

<a onclick="document.getElementById('yourAudioTag').play();">
     <img src="yourSrc.jpg">
</a>

JSFiddle.

You might even be able to put the onclick method into your image tag itself like so:

<img src="yourSrc.jpg" onclick="document.getElementById('yourAudioTag').play();">

You can then use the .pause() method to pause the audio and .load() if you would like to load some new audio. It works similar to the HTML5 video tag.

And if you don't want to use an element for the audio you can declare it like this:

var audio = new Audio('audio_file.mp3'); audio.play();

and use it the same way. Just instead of

document.getElementById('yourAudioTag').play();

you would use

audio.play();

An example of the whole thing would look something like this:

 <a onclick="myAudioFunction('A');">
     <img src="A.jpg">
 </a>
 <a onclick="myAudioFunction('B');">
     <img src="B.jpg">
 </a>
 <script>
     var aAudio = new Audio('a.mp3');
     var bAudio = new Audio('b.mp3');
     function myAudioFunction(letter) {
         if(letter == 'a') {
             aAudio.play();
         } else if(letter == 'b') {
             bAudio.play();
         }
     }
 </script>
2 of 2
3

Interesting question.

Obviously, you should go for a JavaScript solution (like this post). To quote css-tricks.com:

Again unfortunately, we can't tell an <audio> element what to do through CSS, so we'll need JavaScript.

However you have set up quite a nice challenge.

The <audio> element has the ability to show controls. In most browsers, the play button would come first from left in the controls. Thus, if you wrap the appropriate <audio> element inside your letter element (<div>/<img>/ etc.), then, with a little absolute-positioning magic, and low opacity for the <audio>, you could put your letter "over the sound". So, when users click on the letter, they actually click on the play button.

If you use the browser's default controls, then you're browser dependent, as the controls dimensions do differ between browsers. However, you can create custom controls, or use players available in the market (like JPlayer), to have constant dimensions to fit to your letters.

Example and Demo:

The following code will make a click on the letter H play a sound of a horse, as long as you test it in a Chrome browser.

CSS:

div#lH {
  position: absolute;
  width: 30px;
  height: 30px;
  overflow: hidden;
  z-index: 1;
  font-size: 25px;
  text-align:center;
}
audio#aH {
  position: absolute;
  top: -5px;
  left: -5px;
  z-index: 2;
  opacity: 0.01;
}

HTML:

<div id="lH">
  <audio controls id="aH">
    <source src="http://www.w3schools.com/html/horse.ogg" type="audio/ogg">
    <source src="http://www.w3schools.com/html/horse.mp3" type="audio/mpeg">
</audio>
H
</div>

Here's a JSFiddle.