Issue
My html
code is as following:
<input type="text" id="emp" onkeypress="return (event.charCode > 64 &&
event.charCode < 91) || (event.charCode > 96 && event.charCode < 123)" >
It forbids input of any number in the input field. Now, I want to remove this keypress
event on meeting a certain condition in my js
. So I tried executing:
if(condition)
document.getElementById("emp").removeEventListener("keypress");
But it throws an error:
Uncaught TypeError: Failed to execute 'removeEventListener' on 'EventTarget': 2 arguments required, but only 1 present.
How do I remove the event?
Solution
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");
Answered By - Ancesteral
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.