Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

271
Views
how do I remove a keypress event added in <html>?

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?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Here is one way to do it:

<input type="text" id="emp" oninput="console.log(this.value)" >
<button onclick='document.querySelector("input").oninput=null'>remove event</button>

about 4 years ago · Juan Pablo Isaza Report

0

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");
about 4 years ago · Juan Pablo Isaza Report

0

You can make it easier. Instead to remove the event you can set return false if the condition true. In this example if the value length is longer then 3 chars the condition will toggle.

// const i = document.querySelector('input');

function myF(event) 
{
    const charCode = event.keyCode;
    const condition = event.target.value.length < 3 ? false : true;
  
    if (condition) {
      console.log('condition true');
      return false;
    }
  
    if ((charCode > 64 && charCode < 91) || 
        (charCode > 96 && charCode < 123) || 
        charCode == 8) {
      return true; }
    else {
      return false;
    }              
}
<input type="text" id="emp" onkeypress="return myF(event)" >

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!