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

313
Vistas
Uncaught TypeError: Cannot read properties of undefined (reading 'preventDefault')

I trigger onchange attribute in input depending on a specific process. However, even if I "Event" as a parameter, I continue to get the error below. Also, alternatively, I tried Event.StopPropagation () and Return False but the problem is ongoing.

$(function () {
        test();
});

function test(obj) {

        var inputValue = parseInt($(obj).val());

        if (($(obj).val() < 0 || $(obj).val() > 100) && event.keyCode !== 46 // keycode for delete
            && event.keyCode !== 8 // keycode for backspace
        ) {
            event.preventDefault();
            $(obj).val(0);
        } else if (isNaN(inputValue)) {
            event.preventDefault();
            $(obj).val(0);
        }
}
<script src="https://code.jquery.com/ui/1.13.0/jquery-ui.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.0/jquery.min.js"></script>

<div>
<input id="weightInput" type="number" class="form-control weightInput"  data-key="" data-guid="" onchange="test(this)" value="" />
</div>

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

0

You need to pass event as an explicit argument to the onchange argument.

You also shouldn't call it from $(function() { }), since there's no event there.

function test(obj, event) {
  var inputValue = parseInt($(obj).val());
  if (($(obj).val() < 0 || $(obj).val() > 100) && event.keyCode !== 46 // keycode for delete
    &&
    event.keyCode !== 8 // keycode for backspace
  ) {
    event.preventDefault();
    $(obj).val(0);
  } else if (isNaN(inputValue)) {
    event.preventDefault();
    $(obj).val(0);
  }
}
<script src="https://code.jquery.com/ui/1.13.0/jquery-ui.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.0/jquery.min.js"></script>

<div>
  <input id="weightInput" type="number" class="form-control weightInput" data-key="" data-guid="" onchange="test(this, event)" value="" />
</div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

You should be using jQuery's listener to handle the event that isnt defined in your current code. You should also use the events target as your object

/***This here**/
jQuery(document).on('change','#weightInput',test);
function test(event) {
        let obj = event.target
        var inputValue = parseInt($(obj).val());

        if (($(obj).val() < 0 || $(obj).val() > 100) && event.keyCode !== 46 // keycode for delete
            && event.keyCode !== 8 // keycode for backspace
        ) {
            event.preventDefault();
            $(obj).val(0);
        } else if (isNaN(inputValue)) {
            event.preventDefault();
            $(obj).val(0);
        }
}
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