you can play sound by onclick event...insert a button on html.write a function and call it at your button as onclick event.

function playMusic(){
  var music = new Audio('musicfile.mp3');
  music.play();
  }
<input type="button" value="sound" onclick="playMusic()" />

Make sure to give a valid filename.

Answer from Surya Teja on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ html โ€บ html5_audio.asp
HTML Audio
The HTML DOM defines methods, properties, and events for the <audio> element. This allows you to load, play, and pause audios, as well as set duration and volume.
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
How to play audio? - javascript
The user needs first to interact with a page in order to play a sound (this is the regular, expected and user-friendly behavior). 2021-07-04T22:15:46.27Z+00:00 ... This is a quite old question but I want to add some useful info. The topic starter has mentioned that he is "making a game". So for everybody who needs audio for game development there is a better choice than just an More on stackoverflow.com
๐ŸŒ stackoverflow.com
buttonclick - Immediate play sound on button click in HTML page - Stack Overflow
In my HTML page I have 9 images for dialing numbers and one text box that shows the pressed numbers. I want each of those images to immediately play beep sound when users click on them. I tried to ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Playing Audio in HTML - Stack Overflow
Hopefully, in a few years, the HTML5 audio API will be supported accross more browsers, but currently, playing sounds requires either a lot of browser-specific hacks to get things working, or reliance on a browser plugin like flash. More on stackoverflow.com
๐ŸŒ stackoverflow.com
June 19, 2025
๐ŸŒ
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
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ HTML โ€บ Reference โ€บ Elements โ€บ audio
The Embed Audio element - HTML - MDN Web Docs - Mozilla
The HTML element is used to embed sound content in documents. It may contain one or more audio sources, represented using the src attribute or the element: the browser will choose the most suitable one. It can also be the destination for streamed media, using a MediaStream.
๐ŸŒ
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.

๐ŸŒ
W3Schools
w3schools.com โ€บ tags โ€บ tag_audio.asp
HTML audio tag
โฎ Previous Complete HTML Reference ... audio tag. </audio> Try it Yourself ยป ยท The <audio> tag is used to embed sound content in a document, such as music or other audio streams....
๐ŸŒ
Computer Hope
computerhope.com โ€บ issues โ€บ ch000071.htm
How to Create a Link to Play a Sound File in HTML
January 6, 2025 - With HTML (HyperText Markup Language), linking a sound file using a href allows a browser to open and play an audio file if the viewer of your web page has properly configured their Internet browser.
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ html โ€บ html5-audio-tag
HTML audio Tag %%page%% %%sep%% %%sitename%% - GeeksforGeeks
The <audio> tag in HTML5 is used to embed audio content on a webpage. It allows you to play audio files like MP3, OGG, or WAV directly in the browser.
Published ย  June 19, 2025
๐ŸŒ
Programming Head
programminghead.com โ€บ how-to-play-audio-in-html-using-javascript
How to Play Audio in HTML using JavaScript | Audio Player
Which can be Done by JavaScript Click Event or HTML's onClick Attribute. To Play HTML Audio using JavaScript and HTML onCLick Attribute we need to create a Function, Which will run using HTML onClick attribute. As we all know that using HTML's onClick attribute we can Run JavaScript function.
๐ŸŒ
Dobrian
dobrian.github.io โ€บ cmp โ€บ topics โ€บ sample-recording-and-playback-with-web-audio-api โ€บ 1.loading-and-playing-sound-files.html
Loading and Playing Sound Files
The easiest way to load and play a sound file does not actually require any JavaScript at all. HTML includes the <audio></audio> tag, which has a src attribute that you can set to load any sound file you'd like.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ API โ€บ HTMLAudioElement โ€บ Audio
HTMLAudioElement: Audio() constructor - Web APIs | MDN
The Audio() constructor creates and returns a new HTMLAudioElement which can be either attached to a document for the user to interact with and/or listen to, or can be used offscreen to manage and play audio.
๐ŸŒ
Mimo
mimo.org โ€บ glossary โ€บ html โ€บ audio-tag
HTML Audio Tag: Syntax, Usage, and Examples
You can use the HTML audio tag autoplay attribute to play background music or sound effects automatically.
๐ŸŒ
The Sitewizard
thesitewizard.com โ€บ html-tutorial โ€บ play-audio-music-with-html5.shtml
How to Play Music or Audio on a Website with HTML (HTML5) (thesitewizard.com)
November 25, 2019 - This tutorial deals with how you can insert music or some other type of audio file into a web page using HTML, or HTML5 to be exact. The code automatically creates a player that your visitors can use to hear the sound.
๐ŸŒ
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 - If you want to create JavaScript Play Sound On Click then this tutorial is for you. I have created a... Tagged with javascript, webdev, beginners, html.
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ html-play-audio-files-with-html5-audio-tag-451070
Play Audio Files with HTML5 Audio Tag | LabEx
In this lab, you will learn how to work with HTML5 Audio Tag to embed and play audio files directly in web pages.
๐ŸŒ
Programiz
programiz.com โ€บ html โ€บ audio
HTML Audio (With Examples)
The HTML <audio> tag is used to embed a media player which supports audio playback into the HTML page. We use the HTML <audio> tag along with the <source> tag to add the audio player.