I am trying to select the div with class of keys when the respective key is pressed on the keyboard.
I have tried both with JavaScript and jQuery but its not working and shows a null output.
How do i select the div of a particular data-attribute when the key is pressed?
Please see the code below:
html
<div class="row-2">
<div data-key="65" class="keys">
<kbd>A</kbd>
</div>
</div>
Js
window.addEventListener('keydown',function(e){
const here = document.querySelector('.keys[data-key="${e.keycode}"]');
console.log(here);
});
Jquery
$(document).ready(function(){
$('html').on('keydown',function(event){
var press = event.which;
console.log(press);
console.log(document.querySelector('.keys[data-key="${press}"]'));
});