so im working on the drum machine project (alredy passed the test) however my onClick event is working perfectly but onKeyPress doesn’t , when a key is pressed it doesn’t fire and after a click from the mouse a press on any key only repeats the last audio that was clicked by the mouse .
those are my functions :
function handlePress(event) {
event.target.firstChild.load();
event.target.firstChild.play();
}
function handleClick(event) {
event.target.firstChild.load();
event.target.firstChild.play();
}
I don’t think you can target buttons with keypress events, as counter-intuitive as that may seem. If you listen to the event at the document level it should register correctly. I referenced the document directly for that; not sure if it’s best practice but it worked for me.
Since this is being called by an event listener on the body, there is no event.target.firstChild.load() or event.target.firstChild.play(). The only information you have is the key that was pressed. You need to use that information to figure out which audio element to play. It looks like you have given each audio element and id that corresponds to its key. I’d say that might be something to look into.