Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

200
Vistas
I have two input elements with class "inputWithLimit". Why would both be firing simultaneously and logging '0' as val().length?
$(document).ready(function() {
    $(".inputWithLimit").each(() => {
        var inp = this;
        inp.addEventListener("input",
            function (event){
                console.log($(inp).val().length);
            });
    });
})

I've also tried "keyup" and "change" as event handlers, and in both other cases, jquery is doing a strange thing with assigning these listeners. Thanks.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

If you are using jQuery then there is no need to loop through each element and add an event listener. An example which logs the value of each input when you input something.

$(document).ready(function() {
    $(".inputWithLimit").on('input',function() {
        console.log(this.value);
    });
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input class="inputWithLimit">
<input class="inputWithLimit">

about 4 years ago · Juan Pablo Isaza Denunciar

0

The problem is your arrow function. When using arrow function, then you can't use `this'

$(".inputWithLimit").on("input", function() {
  console.log($(this).val().length);
});

I've also made your code shorter.

Demo

$(document).ready(function() {
  $(".inputWithLimit").on("input", function() {
    console.log($(this).val().length);
  });
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input class="inputWithLimit" />

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda