Well the thing is this, I already match up the audio with the keys using the atribute "data-key" doing in this way:
const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`);
const key = document.querySelector(`.key[data-key="${e.keyCode}"]`);
Now, I want to do the same but with the click of the bottom. I already try like you can see at the end of the snippet of JavaScript, adding a loop for all the buttons, but looks awkward and only run with one audio.
function playSound(e) {
const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`);
const key = document.querySelector(`.key[data-key="${e.keyCode}"]`);
if (!audio) return; // this stop the function from running all together.
audio.currentTime = 0; // this rewind the audio to the start
audio.play(key);
key.classList.add("playing");
}
function removeTransition(e) {
if (e.propertyName !== "transform") return; // skip it if it's not a transform
this.classList.remove("playing");
}
const keys = document.querySelectorAll(".key");
keys.forEach((key) => key.addEventListener("transitionend", removeTransition));
document.addEventListener("keydown", playSound);
for (var i = 0; i < document.querySelectorAll(".beat").length; i++) {
document.querySelectorAll(".beat")[i].addEventListener("click", function () {
var clickAudio = new Audio(
"https://sampleswap.org/samples-ghost/DRUMS%20(FULL%20KITS)/DRUM%20MACHINES/808%20Basic/22[kb]conga1.aif.mp3"
);
clickAudio.play();
});
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<link
href="https://fonts.googleapis.com/css2?family=Righteous&display=swap"
rel="stylesheet"
/>
<link
href="https://fonts.googleapis.com/css2?family=Wallpoet&display=swap"
rel="stylesheet"
/>
<title>BEATS MACHINE</title>
</head>
<body>
<div class="container-808">
<div class="name-and-display-container">
<div class="display">
<span class="beats-controler">120.BPM</span>
</div>
<div class="name">
<h1>Rhythm Designer</h1>
<h3>RD-808</h3>
</div>
</div>
<div class="keys-container">
<button data-key="81" class="key beat first-row">
<kbd>Q</kbd><span class="sound">CONGA</span>
</button>
<button data-key="87" class="key beat first-row">
<kbd>W</kbd><span class="sound">HI HAT</span>
</button>
<button data-key="69" class="key beat first-row">
<kbd>E</kbd><span class="sound">HAND CLAP</span>
</button>
<button data-key="82" class="key beat second-row">
<kbd>R</kbd><span class="sound">HIGH TOM</span>
</button>
<button data-key="84" class="key beat second-row">
<kbd>T</kbd><span class="sound">OPEN HIGH HAT</span>
</button>
<button data-key="89" class="key beat second-row">
<kbd>Y</kbd><span class="sound">SNARE</span>
</button>
<button data-key="85" class="key beat third-row">
<kbd>U</kbd><span class="sound">LOW CONGA</span>
</button>
<button data-key="73" class="key beat third-row">
<kbd>I</kbd><span class="sound">CRASH</span>
</button>
<button data-key="79" class="key beat third-row">
<kbd>O</kbd><span class="sound">TAMB</span>
</button>
<audio
data-key="81"
src="https://sampleswap.org/samples-ghost/DRUMS%20(FULL%20KITS)/DRUM%20MACHINES/808%20Basic/22[kb]conga1.aif.mp3"
></audio>
<audio
data-key="87"
src="https://sampleswap.org/samples-ghost/DRUMS%20(FULL%20KITS)/DRUM%20MACHINES/808%20Basic/4[kb]cl_hihat.aif.mp3"
></audio>
<audio
data-key="69"
src="https://sampleswap.org/samples-ghost/DRUMS%20(FULL%20KITS)/DRUM%20MACHINES/808%20Basic/31[kb]handclap.aif.mp3"
></audio>
<audio
data-key="82"
src="https://sampleswap.org/samples-ghost/DRUMS%20(FULL%20KITS)/DRUM%20MACHINES/808%20Basic/17[kb]hightom.aif.mp3"
></audio>
<audio
data-key="84"
src="https://sampleswap.org/samples-ghost/DRUMS%20(FULL%20KITS)/DRUM%20MACHINES/808%20Basic/51[kb]open_hh.aif.mp3"
></audio>
<audio
data-key="89"
src="https://sampleswap.org/samples-ghost/DRUMS%20(FULL%20KITS)/DRUM%20MACHINES/808%20Basic/8[kb]snare.aif.mp3"
></audio>
<audio
data-key="85"
src="https://sampleswap.org/samples-ghost/DRUMS%20(FULL%20KITS)/DRUM%20MACHINES/80s%20Drum%20Machine/16[kb]80s-LOWCONGA.aif.mp3"
></audio>
<audio
data-key="73"
src="https://sampleswap.org/samples-ghost/DRUMS%20(FULL%20KITS)/DRUM%20MACHINES/80s%20Drum%20Machine/83[kb]80s-CRASH1.aif.mp3"
></audio>
<audio
data-key="79"
src="https://sampleswap.org/samples-ghost/DRUMS%20(FULL%20KITS)/DRUM%20MACHINES/80s%20Drum%20Machine/20[kb]80s-TAMB1.aif.mp3"
></audio>
</div>
</div>
<script src="index.js"></script>
</body>
</html>