Get HTML Table Data In Javascript (Simple Examples)

Welcome to a tutorial on how to get HTML table data in Javascript. So you want to extract values from an HTML table in Javascript?

One of the easiest ways to get data from an HTML table is to select all the cells and loop through them:

  • <table id="demo">
  • var cells = document.querySelectorAll("#demo td");
  • for (let c of cells) { console.log(c.innerHTML); }

That covers the quick basics, but read on for more examples.

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

TLDR – QUICK SLIDES

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

QUICK NOTES

If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

 

GET HTML TABLE DATA

All right, let us now get into the examples of getting data from an HTML table in Javascript.

 

DUMMY TABLE

0-dummy.html
<table id="demo">
  <tr><th>Name</th> <th>Email</th></tr>
  <tr><td>Joa Doe</td> <td>joa@doe.com</td></tr>
  <tr><td>Job Doe</td> <td>job@doe.com</td></tr>
  <tr><td>Joe Doe</td> <td>joe@doe.com</td></tr>
  <tr><td>Jon Doe</td> <td>jon@doe.com</td></tr>
  <tr><td>Joy Doe</td> <td>joy@doe.com</td></tr>
</table>

For the following examples, we will work with this table with a list of dummy users.

 

EXAMPLE 1) LOOP THROUGH ROWS & CELLS

1-loop.js
// (A) GET TABLE ROWS
var rows = document.querySelectorAll("#demo tr");

// (B) LOOP THROUGH ROWS
for (let row of rows) {
  // (B1) GET CELLS
  let cells = row.querySelectorAll("td");
  if (cells.length == 0) { cells = row.querySelectorAll("th"); }

  // (B2) LOOP THROUGH CELLS
  for (let cell of cells) { console.log(cell, cell.innerHTML); }
}

This is pretty much an “expanded version” of the snippet in the introduction.

  • (A) In plain English, var rows = document.querySelectorAll("#demo tr") means “get all <tr> elements that are within id="demo"“.
  • (B & B1) Loop through each row of the table, use let cells = row.querySelectorAll("td") to get the cells of the current row.
  • (B2) for (let cell of cells) { ... } Loop through the cells of the current row.

 

 

EXAMPLE 2) TABLE CELL VALUES INTO AN ARRAY

2-array.js
// (A) GET TABLE ROWS + INIT DATA ARRAY
var rows = document.querySelectorAll("#demo tr"),
    data = [];

// (B) LOOP THROUGH ROWS
for (let row of rows) {
  // (B1) ADD DATA ROW
  data.push([]);
  let r = data.length - 1;

  // (B2) GET CELLS
  let cells = row.querySelectorAll("td");
  if (cells.length == 0) { cells = row.querySelectorAll("th"); }

  // (B3) LOOP THROUGH CELLS
  for (let cell of cells) { data[r].push(cell.innerHTML); }
}
console.log(data);

If you need to extract the values into an array – This is pretty much the same “loop through rows and cells”, except that we collect the values into a nested array along the way. For those who are lost, the array will look something like this:

var data = [
  ["Name", "Email"],
  ["Joa Doe", "joa@doe.com"],
  ["Job Doe", "job@doe.com"],
  ...
];

 

 

EXAMPLE 3) TABLE CELL VALUES INTO AN OBJECT

3-object.js
// (A) INIT DATA
var rows = document.querySelectorAll("#demo tr"),
    data = {}, map = [], cells;

// (B) LOOP THROUGH HEADER CELLS
cells = rows[0].querySelectorAll("th");
for (let header of cells) {
  data[header.innerHTML] = [];
  map.push(header.innerHTML);
}

// (C) LOOP THROUGH DATA ROWS
for (let r=1; r<rows.length; r++) {
  cells = rows[r].querySelectorAll("td");
  for (let [i,cell] of Object.entries(cells)) {
    data[map[i]].push(cell.innerHTML);
  }
}
console.log(data);

If you prefer to “sort by the table header” instead, some “data yoga” is necessary.

  • (A) Pretty much the same “get all table rows”, but take note
    • data = {} is an object, not an array.
    • We will use map = [] to arrange the data.
  • (B) Start with some “data init” in the header cells of the first row.
    • data = { "Name" : [], "Email" : [] }
    • map = ["Name", "Email"]
  • (C) Loop through the data rows and populate the data object.

For those who are lost once again, this is the final result:

var data = {
  "Name" : ["Joa Doe", "Job Doe", ...],
  "Email" : ["joa@doe.com", "job@doe.com", ...]
};

 

 

EXTRA BITS & LINKS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

LINKS & REFERENCES

 

INFOGRAPHIC CHEAT SHEET

Javascript Get HTML Table Data (click to enlarge)

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *