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

269
Views
¿Cómo elimino un evento de pulsación de tecla agregado en <html>?

Mi código html es el siguiente:

 <input type="text" id="emp" onkeypress="return (event.charCode > 64 && event.charCode < 91) || (event.charCode > 96 && event.charCode < 123)" >

Prohíbe la entrada de cualquier número en el campo de entrada. Ahora, quiero eliminar este evento de keypress de tecla al cumplir con una determinada condición en mi js . Así que intenté ejecutar:

 if(condition) document.getElementById("emp").removeEventListener("keypress");

Pero arroja un error:

TypeError no capturado: no se pudo ejecutar 'removeEventListener' en 'EventTarget': se requieren 2 argumentos, pero solo 1 presente.

¿Cómo elimino el evento?

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

0

Aquí hay una manera de hacerlo:

 <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

El atributo de las etiquetas HTML funciona como una función frente a un detector de eventos. Para lograr lo que busca, solo necesita editar o eliminar ese atributo

 if(condition) document.getElementById("emp").setAttribute("onkeypress", ""); if(condition) document.getElementById("emp").removeAttribute("onkeypress");
about 4 years ago · Juan Pablo Isaza Report

0

Puedes hacerlo más fácil. En lugar de eliminar el evento, puede configurar return false si la condición es verdadera. En este ejemplo, si la longitud del valor es mayor a 3 caracteres, la condición cambiará.

 // 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!