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

133
Views
Getting value from input using .value is returning ""

I want to get the value inside the input box and display it on the console

<form action="#">
    name : <input type="text" id="name">
</form>
let name = document.getElementById('name').value;
console.log(name);

But it is returning empty in console log. How can I get the value from input box?

Thanks

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

0

You can use keyup event on input for when you type in input it will display value in console like below example:

const nameInput = document.getElementById('name');

nameInput.addEventListener("keyup", (e)=> {
  console.log(nameInput.value)
});
<form action="#">
    name : <input type="text" id="name">
</form>

about 4 years ago · Juan Pablo Isaza Report

0

You are getting a blank value of the input field because you have not added any value attribute in the input field. So you have to add a value attribute in the input field.

let name = document.getElementById('name').value;
console.log(name);
<form action="#">
    name : <input type="text" id="name" value="my custom value">
</form>

about 4 years ago · Juan Pablo Isaza Report

0

Add a button to initiate form submit

<form action="#" id="form1">
    name : <input type="text" id="name">
    <input type="submit" value="Submit">
</form>

Now add event listener for Submit event

document.getElementById("form1").addEventListener("submit", function(e) {
    // Prevent default behavior to avoid page refresh
    e.preventDefault();
    let name = document.getElementById('name').value;
    console.log(name);
})
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!