What is wrong in js code?

In the object res is Not output data, there is no reaction to the event.
In the console, in the properties of the res object marked onkeypress: null

Index

<input id="inp">
<div id="res" style="height: 20px;"></div>


JS

var inp = document.getElementById('inp');
var res = document.getElementById('res');

function func2(e){
 var code = e.keyCode;
 var key = String.fromCharCode(code);
 Res. innerHTML = 'your code:' + code + '; key was pressed: '+ key;
}

inp.onkeypress = func2;
March 19th 20 at 08:56
1 answer
March 19th 20 at 08:58
Solution
Code needs to be at the end of the page, or load after load all html elements. Res. onkeypress will be null, because the press event you ask for inp
You are right. Long as our father remember that the scripts before the closing Badi (well still have in my defer ...). But then I wedged a new fragment khtml protupila and this point, and the script remained above. Need bytt carefully. Thank you. - Stephen commented on March 19th 20 at 09:01

Find more questions by tags JavaScript