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

257
Views
Why is the input field value not showing in the table?

I'm making a dynamic HTML table using JavaScript, to which you can add first and last names. But when I click on the submit button, it creates empty cells without an input field value.

HTML

<input type="text" id="name" placeholder="First Name">
<input type="text" id="lastname" placeholder="Last Name">
<input type="button" id ="submit" value="Submit" >

<table border="1" id="table">
    <tr>
    <th>First Name</th>
    <th>Last Name</th>
    </tr>
</table>

JavaScript

let name = document.getElementById("name").value;
let lastname = document.getElementById("lastname").value;
let submit = document.getElementById("submit");
let table = document.getElementById("table");

var rowNum = 1;

submit.addEventListener("click" , function(){
  var row = table.insertRow(rowNum);
  var cell0 = row.insertCell(0);
  var cell1 = row.insertCell(1);
  cell0.innerHTML = name;
  cell1.innerHTML = lastname;
  rowNum++;
});

Enter image description here

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

0

You need to get the inputs value after submit, not in the initial load of the script, as the values will be just empty string on script's initial load.

You just need to add name and lastname variables inside the scope of the click event listener.

let submit = document.getElementById("submit");
let table = document.getElementById("table");

var rowNum = 1;

submit.addEventListener("click" , function(){
 let name = document.getElementById("name").value;
let lastname = document.getElementById("lastname").value;
 var row = table.insertRow(rowNum);
 var cell0 = row.insertCell(0);
 var cell1 = row.insertCell(1);
 cell0.innerHTML = name;
 cell1.innerHTML = lastname;
 rowNum++;
});
<input type="text" id="name" placeholder="First Name">
<input type="text" id="lastname" placeholder="Last Name">
<input type="button" id ="submit" value="Submit" >
    
<table border="1" id="table">
     <tr>
     <th>First Name</th>
     <th>Last Name</th>
     </tr>
</table>

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!