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.

 

 

TLDR – QUICK SLIDES

[web_stories_embed url=”https://code-boxx.com/web-stories/javascript-html-table-data/” title=”Get HTML Table Data In Javascript” poster=”https://code-boxx.com/wp-content/uploads/2021/11/STORY-JS-20230518.webp” width=”360″ height=”600″ align=”center”]

Fullscreen Mode – Click Here

 

TABLE OF CONTENTS

 

 

GET HTML TABLE DATA

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

 

DUMMY TABLE

x-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", ...]
};

 

 

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

 

SORRY FOR THE ADS...

But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.

Buy Me A Coffee Code Boxx eBooks

 

EXAMPLE CODE DOWNLOAD

Click here for the source code on GitHub gist, just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

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!