Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

270
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda