How To Add Table Rows & Cells In Javascript (Simple Examples)

Welcome to a quick tutorial on how to add HTML table rows and cells in Javascript. Need to dynamically update an HTML table using Javascript?

To add new rows and cells to an HTML table in Javascript:

  1. Get the table – var table = document.getElementById("TABLE");
  2. Add a new row – var row = table.insertRow();
  3. Append the cells – var cell = row.insertCell();
  4. Set cell content – cell.innerHTML = "TEXT";

That should cover the 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.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes Add Rows Cells Useful Bits & Links
The End

 

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.

 

 

ADD ROWS & CELLS

All right, let us now get into the examples of adding rows and cells to HTML tables.

 

1) INSERT ROWS & CELLS

1-add-row.html
<!-- (A) HTML TABLE -->
<table id="demoA">
  <tr> <td>A</td> <td>B</td> </tr>
</table>

<!-- (B) ADD ROWS CELLS -->
<script>
function addRow (top) {
  // (B1) GET TABLE
  var table = document.getElementById("demoA");

  // (B2) INSERT ROW
  if (top) { var row = table.insertRow(0); }
  else { var row = table.insertRow(); }

  // (B3) INSERT CELLS
  var cell = row.insertCell();
  cell.innerHTML = "AA";
  cell = row.insertCell();
  cell.innerHTML = "BB";
}
</script>
<input type="button" value="Add Bottom" onclick="addRow()"/>
<input type="button" value="Add Top" onclick="addRow(1)"/>
A B

Look no further, these are the same steps as the introduction snippet – Get the table, add a row, append the cells. But take extra note:

  • By default, insertRow() will insert to the bottom of the table.
  • Use insertRow(0) to add to the top of the table instead.

Yes, insertRow() takes in an “insert position” parameter. We can do some simple calculations if you want to insert a row into the middle of the table:

  • var pos = Math.ceil(table.rows.length / 2);
  • var row = table.insertRow(pos);

 

 

2) MANUAL HTML STRING

2-html-string.html
<!-- (A) HTML TABLE -->
<table id="demoB">
<tr> <td>A</td> <td>B</td> </tr>
</table>

<!-- (B) ADD ROWS CELLS -->
<script>
function addRow () {
  var table = document.querySelector("#demoB tbody");
  table.innerHTML = "<tr><td>TOP</td><td>AA</td></tr>" + table.innerHTML;
  table.innerHTML += "<tr><td>BOTTOM</td><td>BB</td></tr>";
}
</script>
<input type="button" value="Add Rows" onclick="addRow()"/>
A B

For you guys who prefer to do it the “manual HTML way”, this is the alternative. But a small word of advice – Directly changing the innerHTML is probably not that good for performance, especially when we have a massive table.

 

 

USEFUL BITS & LINKS

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

 

WHICH IS BETTER?

Personally, I am leaning towards the “object-oriented” create new rows and cells way. That is just a lot more convenient and easier, without having a write manual HTML code. But both methods are correct – Use whichever works best for you.

 

LINKS & REFERENCES

 

INFOGRAPHIC CHEAT SHEET

Add Table Rows Cells In Javascript (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.