How can i call the event onkeypress in a div? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How can i call the event onkeypress in a div?

25th Nov 2018, 2:26 AM
Johannes
Johannes - avatar
2 Answers
+ 2
Hey! Onkeypress has been deprecated, you can use onkeydown instead. Let's say that the div has a 'div1' id, here's how to do it: document.getElementById('div1').addEventListener('keydown', function() { //your code goes here }
25th Nov 2018, 10:45 AM
Hamza Alalach
Hamza Alalach - avatar
+ 1
You can add any attribute to the elememt itself. <div onkeyup="function()"></div>
25th Nov 2018, 11:37 AM
Dominique Abou Samah
Dominique Abou Samah - avatar