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

227
Vistas
why does input.value is returning undefined when call it in alert

I have a text input and I want to alert the input.value after press enter

so I have made this code HTML :

<input type="text" id="food">

js :

$('#food').on('keypress',function(e) {
 if(e.which == 13) {
    const food = $('#food').value;
    alert(food);
 }
});

in the alert, it's showing undefined. in addition, there is no console error in the console

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

0

You should use val() method to get the value of the input in jquery

$('#food').val()

value is used in vanilla JS, not in jquery.

$('#food').on('keypress', function(e) {
  if (e.which == 13) {
    const food = $('#food').val();
    console.log(food);
    // alert( food );
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="food">

about 4 years ago · Juan Pablo Isaza Denunciar

0

You need to use a function on jQuery to get that value.

$('#food').val();

$('#food').attr('value');
about 4 years ago · Juan Pablo Isaza Denunciar

0

It is not const food = $("#food").value but const food = $("#food").val()

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