I'm using switch() to play sounds when certain keys are pressed, but I would like to get the sound loop to stop and reset when letting up on the key. Here is my code:
$(function() {
$(document).on("keydown", function(key) {
switch(parseInt(key.which, 10)) {
case 65:
$("#sound").get(0).play();
break;
case 83:
$("#sound2").get(0).play();
break;
case 68:
$("#sound3").get(0).play();
break;
case 70:
$("#sound4").get(0).play();
break;
};
}).on("keyup", function() {
$(this).pause();
$(this).currentTime=0;
});
});
I think my problem lies in using $(this) within the keyup handler, but I don't want to specify each sound id as a variable as I will ultimately have many of them. Is there a way to do this simply?
Thanks for the help!