Skip to content
Sign upLog in
← Back to Community

Add sound to a button in Html and JavaScript!

Profile icon
[deleted]

Learn how to Add sound when we click a button on a website!

Let's get started!

Add a button in HTML!

<button onclick="sound()">Play sound :)</button>

JavaScript!

<script src="file_name.js"></script>

In the above code, we just linked an external JS file

As we've coded above, when we click on the button, a function gets executed, so let's create the function named sound!

Note: Upload the sound you want to play in the repl

function sound(){ var snd = new Audio('sound_file_name.mp3')//wav is also supported snd.play()//plays the sound }

Upvote if you find it helpful :)

Voters
Profile icon
skenne28
Profile icon
XEROxMEXICANO
Profile icon
CATSAG7PRO
Comments
hotnewtop
Profile icon
ItamarCohen28

BRUHHHHHHHHHHHHHHHHHHHHHHH

YOU SCARED THE # **HELL ** OUTTA ME!

Profile icon
[deleted]

coooooooooooooooooooollll!!!!!!!

Profile icon
ch1ck3n

@Hacky
Forgot to switch accounts?

Profile icon
GlitchWarrior

@ch1ck3n
HAHAHAHHA