In this article, I'll show you how to create a Sound Board in JavaScript with only 3 simple lines of code.
Before that, create a bunch of buttons and style those quickly.
I have added 5 buttons for 5 different kinds of sounds.
<div class="container">
<div class="center">
<button id="applause">applause</button>
<button id="boo">boo</button>
<button id="victory">victory</button>
<button id="nope">nope</button>
<button id="gasp">gasp</button>
</div>
</div>
.container {
position: absolute;
top: 50%;
left: 50%;
margin-top: -50px;
margin-left: -50px;
width: 100px;
height: 100px;
}
.center {
display: flex;
justify-content: center;
align-items: center;
}
button {
min-width: 80px;
height: 40px;
margin-left: 10px;
border-radius: 4px;
border: 1px solid plum;
cursor: pointer;
color: purple;
font-weight: 600;
}
Now, I added the function to play the sound when a specific button is pressed.
function play_sound(clicked_id) {
var audio = new Audio(clicked_id + ".mp3");
audio.play();
}
and call this function to a button onClick method
...
<button id="applause" onClick="play_sound(this.id)">applause</button>
...
That's it. You get a sound board with just 3 lines of code 🔊
Thank you for reading!
Feel free to connect on Twitter
Oldest comments (10)
Hi! Well done! It is fantastic and gave me some ideas were use it for the fan!
Hi Adriana, Thank you!
I am following this video to get JS project ideas. You can also checkout my other blogs, I have implemented 2 other projects from this video.
I hope you find it interesting and helpful!
Thank you! It is very helpeful! Have a wonderful week!
Awesome!
Thank you!
So simple! I'll try to make a mini virtual stream deck
Great! Looking forward to it!
Fun project!!
Learning in a fun way is always interesting 😁
Thank you!
Well said. Your welcome!