lundi 22 février 2021

different vars and ifs for different videos not working

I'm trying to create the function that if a key is pressed, a video appears. I have managed to do it for one key and it works fine:

 var video = document.getElementById('b1');   
document.addEventListener('keydown', (e) => {
   if(e.key === "b") {
     video.play(); video.currentTime = 0;
   }
});

But i want to do this for several different keys, (26 to be exact). I figured that what I needed to do was copy the same code and place it below, like this:

var video = document.getElementById('b1');   
document.addEventListener('keydown', (e) => {
   if(e.key === "b") {
     video.play(); video.currentTime = 0;
   }
});

var video = document.getElementById('d1');   
document.addEventListener('keydown', (e) => {
   if(e.key === "d") {
     video.play(); video.currentTime = 0;
   }
});

But i get the error: "'video' is already defined. Do not redeclare". I get that what's wrong is the composition of the code but I spent a while looking for the right way to do it and couldn't find it. Any help would be really appreciated!

Aucun commentaire:

Enregistrer un commentaire