DEV Community

Cover image for Create a Sound Board in 3 lines of code
Aneeqa Khan
Aneeqa Khan

Posted on

Create a Sound Board in 3 lines of code

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>
Enter fullscreen mode Exit fullscreen mode
.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;
}
Enter fullscreen mode Exit fullscreen mode

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();
  }
Enter fullscreen mode Exit fullscreen mode

and call this function to a button onClick method

...
  <button id="applause" onClick="play_sound(this.id)">applause</button>
...
Enter fullscreen mode Exit fullscreen mode

That's it. You get a sound board with just 3 lines of code 🔊

Thank you for reading!
Feel free to connect on Twitter

Top comments (10)

Collapse
 
suman373_30 profile image
Suman Roy •

Fun project!!

Collapse
 
aneeqakhan profile image
Aneeqa Khan •

Learning in a fun way is always interesting 😁
Thank you!

Collapse
 
suman373_30 profile image
Suman Roy •

Well said. Your welcome!

Collapse
 
lexplt profile image
Lexy Plt •

So simple! I'll try to make a mini virtual stream deck

Collapse
 
aneeqakhan profile image
Aneeqa Khan •

Great! Looking forward to it!

Collapse
 
adrianacamarotto profile image
Adriana Camarotto •

Hi! Well done! It is fantastic and gave me some ideas were use it for the fan!

Collapse
 
aneeqakhan profile image
Aneeqa Khan •

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!

Collapse
 
adrianacamarotto profile image
Adriana Camarotto •

Thank you! It is very helpeful! Have a wonderful week!

Collapse
 
thatsameer profile image
ThatSameer •

Awesome!

Collapse
 
aneeqakhan profile image
Aneeqa Khan •

Thank you!