a_l_e_x
a_l_e_x

Reputation: 400

I can't see the number of rows in the table correctly

I'm having a problem in correctly displaying the number of rows in the table.

In particular, rowCount is always equal to 1.

Can anyone kindly help me?

let table = document.getElementById("my-table");
allart.Items.forEach(function (item) {
let child = document.createElement("tr");
child.innerHTML = `
<td>${item.id}</td>
<td>${item.title}</a></td>
<td>${item.brand}</td>
table.appendChild(child);
}

var table = document.getElementById(my-table).tBodies[0];
var rowCount = table.rows.length;
<table id = "my-table" width = "90%">
         <tr>
             <th> Id </th>
             <th> Title </th>
             <th> Brand </th>
         </tr>

 </table>

Regarding the javascript code, I only put the code of interest because the whole code is too long

Upvotes: 1

Views: 45

Answers (1)

Shayan Faghani
Shayan Faghani

Reputation: 240

EDITED: tBodies will give you how many tbody tags () you are having in your HTML and it is not related to how many rows. As the reference: https://www.w3schools.com/html/tryit.asp?filename=tryhtml_table3

If you want to see how many rows you have in your table you can use this code:

var x = document.getElementById("my-table").rows.length;

Upvotes: 2

Related Questions