Skip to content

Commit 4eb2d69

Browse files
committed
Plays sound on keypress
1 parent c48ef2d commit 4eb2d69

1 file changed

Lines changed: 8 additions & 6 deletions

File tree

01 - JavaScript Drum Kit/index-START.html

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,7 @@
5858
<audio data-key="76" src="sounds/tink.wav"></audio>
5959

6060
<script>
61-
let key = document.querySelector(".key")
62-
let keys = document.querySelector(".keys")
63-
64-
const keyMap {
61+
const keyMap = {
6562
'a': '65',
6663
's': '83',
6764
'd': '68',
@@ -73,10 +70,15 @@
7370
'l': '76',
7471
}
7572

76-
console.log(keys)
77-
document.addEventListener("keypress", e => {
73+
function playAudio(key) {
74+
const sound = document.querySelector(`audio[data-key="${keyMap[key]}"]`)
75+
sound.play()
76+
}
7877

78+
document.addEventListener("keypress", e => {
79+
playAudio(e.key)
7980
})
81+
8082
</script>
8183

8284

0 commit comments

Comments
 (0)