The attribute on HTML tags operates like a function vs an event listener. In order to achieve what you’re after you just need to edit or remove that attribute
if(condition)
document.getElementById("emp").setAttribute("onkeypress", "");
if(condition)
document.getElementById("emp").removeAttribute("onkeypress");
CLICK HERE to find out more related problems solutions.