-4

I'm trying to select a specific audio element based on the key that's pressed. The issue is the compiler is reading the variable as a string. How can I get it to read it as a variable?

var hail = e.keyCode 
 var afile = document.querySelector("audio[data-id='hail']")
Liam
  • 27,717
  • 28
  • 128
  • 190

1 Answers1

0

You can use Javascript template strings :

var hail = e.keyCode 
 var afile = document.querySelector(`audio[data-id=${hail}]`)
Dmitriy
  • 1,211
  • 1
  • 11
  • 28