Learn to Code via Tutorials on Repl.it!

← Back to all posts
Add sound to a button in Html and JavaScript!
[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 :)

Comments
hotnewtop
ItamarCohen28 (10)

BRUHHHHHHHHHHHHHHHHHHHHHHH

YOU SCARED THE # HELL OUTTA ME!

[deleted]

coooooooooooooooooooollll!!!!!!!

ch1ck3n (1616)

@Hacky Forgot to switch accounts?