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

226
Views
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 answers
Answer question

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 Report

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 Report

0

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

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!