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

163
Views
How can I make the serial number of the dynamic table row to be equal to the serial number of the object in the array?

I am trying to create a table where each row will be an object in an array. Logically it works, but it gives me the number of rows in the table equal to the number of objects in the array each time I perform the function. Instead, I need the serial number of the row to be equal to the serial number of the object in the array. How can I achieve this? (* beginner's class project. can't use advanced js/libraries).

<html>

<head>
  <style>
    table,
    th,
    td,
    tr {
      border: 1px solid black;
    }
  </style>
</head>

<body>
  <form>
    <input id="firstname" type="text" placeholder="firstname"></input><br>
    <input id="lastname" type="text" placeholder="lastname"></input><br>
  </form>
  <button onclick="myFunc()">calc</button>
  <div></div>
</body>
<script>
  let properties = [
    'firstname',
    'lastname',
  ]
  let form = document.querySelector('form')
  let div = document.querySelector('div')
  let user = {}
  let users = []

  function myFunc() {
    users.push(user)
    for (let prop of properties) {
      user[prop] = form.elements[prop].value
    }
    for (let user of users) {
      //HERE is my problem. do i need to create separate function to calculate nubers and another one to render the table? 
      let tr = document.createElement('tr')
      for (let key in user) {
        let td = document.createElement('td')
        td.innerHTML = user[key]
        tr.appendChild(td)
      }
      let table = document.createElement('table')
      table.appendChild(tr)
      div.appendChild(table)
      form.reset()
      console.log(users.length)
    }
  }
</script>

</html>

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

0

I'm not sure if I understood you correctly but I think you were pretty close. The thing is that, how it currently is, you never delete items from the object, you only add. So you don't have to render the whole table from the beginning therefore you don't need the for loop.

<html>

<head>
  <style>
    table,
    th,
    td,
    tr {
      border: 1px solid black;
    }
  </style>
</head>

<body>
  <form>
    <input
      id="firstname"
      type="text"
      placeholder="firstname"></input><br>
    <input
      id="lastname"
      type="text"
      placeholder="lastname"></input><br>
  </form>
  <button
    onclick="myFunc()">calc</button>
  <div>
  </div>
</body>
<script>
  let properties = [
    'firstname',
    'lastname',
  ]
  let form = document.querySelector('form')
  let div = document.querySelector('div')
  let user = {}
  let users = []

  function myFunc() {
    users.push(user)
    for (let prop of properties) {
      user[prop] = form.elements[prop].value
    }
    let tr = document.createElement('tr')
    for (let key in user) {
      let td = document.createElement('td')
      td.innerHTML = user[key]
      tr.appendChild(td)
    }
    let table = document.querySelector("table")
    if (table === null) {
      table = document.createElement('table');
    }
    table.appendChild(tr)
    div.appendChild(table)
    form.reset()
    console.log(users.length);
  }
</script>

</html>

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!