Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript run function keydown

I cant find an answer to this that I understand. I want with JavaScript (not jQuery) to make it so if the with the id boxFilm is visible and I click on "SPACE" (keycode 32), the function togglep(); runs.

How do I do this? I've tried many things but haven't succeeded :(

like image 646
Jeremy Karlsson Avatar asked Aug 04 '26 18:08

Jeremy Karlsson


1 Answers

function togglep(){
    alert("Hi");
}

document.body.onkeydown = function(event){
    event = event || window.event;
    var keycode = event.charCode || event.keyCode;
    if(keycode === 32){
        togglep();
    }
}

try it here: http://jsfiddle.net/GuvRP/1/

like image 169
wong2 Avatar answered Aug 06 '26 07:08

wong2