Trying to utilize .keyCode in JavaScript to identify a pressed key, but the console consistently displays null after each key press.
Below is the relevant CSS code:
<audio data-key="65" src="sounds\crash.mp3"></audio>
<audio data-key="83" src="sounds\kick-bass.mp3"></audio>
<audio data-key="68" src="sounds\snare.mp3"></audio>
<audio data-key="70" src="sounds\tom-1.mp3"></audio>
<audio data-key="71" src="sounds\tom-2.mp3"></audio>
<audio data-key="72" src="sounds\tom-3.mp3"></audio>
<audio data-key="74" src="sounds\sound-effect-thriller-1-108404.mp3"></audio>
<audio data-key="75" src="sounds\tom-4.mp3"></audio>
<audio data-key="76" src="sounds\flautatravessera-46420.mp3"></audio>
Here is the attempt at JavaScript code:
window.addEventListener('keydown', function(e){
const audio=this.document.querySelector('audio[data-key="${e.keyCode}"]')
console.log(audio);
audio.play();
}
)
The issue arises when console.log(audio)
returns null.