Play sound on key press - JavaScript
I don't know what is wrong with my code the sound is not playing when I am pressing the keys. for(var i=0;i<document.querySelectorAll(".drum").length;i++) { document.querySelectorAll("button")[i].addEventListener("click",function() { var buttoninnerhtml = this.innerHTML; makesound(buttoninnerhtml); }); } document.addEventListener("keydown",function(event){ makesound(event,key); }); function makesound(key) { switch (key) { case "w": var audio = new Audio('sounds/tom-1.mp3'); audio.play(); break; case "a": var audio = new Audio('sounds/tom-2.mp3'); audio.play(); break; case "s": var audio = new
0 upvotes
View original discussion ↗
Comments
Sign in or sign up to comment and upvote.
No comments yet.