Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (7k points)

I would like to know how can I use onkeydown and onkeyup events in javascript correctly with an event listener.

1 Answer

0 votes
by (13.1k points)

You can do something like this:

$(document).ready(function() {

    document.onkeydown = function(event) {

    if(event.keyCode=="38")

    console.log("Up key pressed");

    else if(event.keyCode=="40")

        console.log("Down key pressed");

   };

});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Interested in full stack development? Check out the full stack developer course from Intellipaat. 

Related questions

0 votes
1 answer
asked Feb 25, 2021 in Web Technology by Jake (7k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...