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

111
Views
Joining JS array with HTML table

I created a table with HTML and I need to join that table with an array from JS:

var array = [
  ["A4", "Audi", "2015", "1234"],
  ["A3", "Audi", "2011", "1542"],
  ["335i", "BMW", "2012", "9874"],
  ["440d", "BMW", "2015", "1975"],
  ["Civic", "Honda", "2002", "6574"],
]
console.log(array)

table = document.getElementById("myTable");
for (var i = 0; i < array.length; i++) {
  // create a new row
  var newRow = table.insertRow(table.length);
  for (var j = 0; j < array[i].length; j++) {
    // create a new cell
    var cell = newRow.insertCell(j);

    // add value to the cell
    cell.innerHTML = array[i][j];
  }
}
<table id="myTable">
  <caption id=a><b>Automobiliai</caption></b>
    <tr>
      <th>Modelis</th>
      <th>Gamintojas</th>
      <th>Metai</th>
      <th>Variklio Numeris</th>
    </tr>
</table>

But I get the error in console: "Uncaught TypeError: Cannot read properties of null (reading 'insertRow')" What is wrong there?

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

0

You can do this by looping through the country list and creating an HTML string. Then you can put that inside the tbody using a class or id selector. Here is an example

var country = ["Norway", "Sweden", "Denmark"];
    var capital = ["Oslo", "Stockholm" , "Copenhagen"]

    var bodyString = '';
    $.each(country, function(index, ctry) {
        bodyString += ('<tr><td>'+ctry+'</td><td>'+capital[index]+'</td></tr>');
    });
    $('.countriesTable tbody').html(bodyString);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
</head>
<body>

  <table class="countriesTable">
      <thead>
          <tr><th>Country</th><th>Capital</th>
      </thead>
      <tbody>
      
      </tbody>
  </table>
</body>
</html>

about 4 years ago · Juan Pablo Isaza Report

0

Looks like your code works. Here is an alternative approach, using Array.reduce to create the rows and insertAdjacentHTML to append the rows.

document.querySelector(`#myTable`).insertAdjacentHTML(
  `beforeEnd`, [
    ["A4", "Audi", "2015", "1234"],
    ["A3", "Audi", "2011", "1542"],
    ["335i", "BMW", "2012", "9874"],
    ["440d", "BMW", "2015", "1975"],
    ["Civic", "Honda", "2002", "6574"],
  ].reduce( (acc, [Modelis, Gamintojas, Metai, VariklioNumeris]) =>
    `${acc}<tr><td>${Modelis}</td><td>${Gamintojas}</td><td>${
        Metai}</td><td>${VariklioNumeris}</td></tr>`, ``)
);
#myTable th:nth-child(n+3):nth-child(-n+4),
#myTable td:nth-child(n+3):nth-child(-n+4) {
  text-align: center;
}
<table id="myTable">
  <caption id=a><b>Automobiliai</caption></b>
    <tr>
      <th>Modelis</th>
      <th>Gamintojas</th>
      <th>Metai</th>
      <th>Variklio Numeris</th>
    </tr>
</table>

about 4 years ago · Juan Pablo Isaza Report

0

I think, I found your problem, actually your script is loading before html

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>

    <table id="myTable">
      <caption id=a><b>Automobiliai</caption></b>
      <tr>
        <th>Modelis</th>
        <th>Gamintojas</th>
        <th>Metai</th>
        <th>Variklio Numeris</th>
      </tr>
    </table>

    <script type="text/javascript">
        var array = [
        ["A4", "Audi", "2015", "1234"],
        ["A3", "Audi", "2011", "1542"],
        ["335i", "BMW", "2012", "9874"],
        ["440d", "BMW", "2015", "1975"],
        ["Civic", "Honda", "2002", "6574"],
        ["A4", "Audi", "2015", "1234"],
        ["A3", "Audi", "2011", "1542"],
        ["335i", "BMW", "2012", "9874"],
        ["440d", "BMW", "2015", "1975"],
        ["Civic", "Honda", "2002", "6574"],
        ]
        console.log(array)

        table = document.getElementById("myTable");
        for(var i = 0; i < array.length; i++)
        {
        // create a new row
        var newRow = table.insertRow(table.length);
        for(var j = 0; j < array[i].length; j++)
        {
        // create a new cell
        var cell = newRow.insertCell(j);

        // add value to the cell
        cell.innerHTML = array[i][j];
        }
        }
    </script>
</body>
</html>

I think, I found your problem, actually your script is loading before looping elements

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!