How to Extract HTML Table Data in JavaScript

How to Extract HTML Table Data in JavaScript


In this tutorial we will create a How to Extract HTML Table Data in JavaScript. This tutorial purpose is to teach you how to extract table data. This will cover all the basic function that will that let you extract the html table data. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system or application if you want to fetch the html table data. I will give my best to provide you the easiest way of creating this program Extract HTML table data. So let's do the coding.

Before we get started:

This is the link for the template that i used for the layout design

Creating The Interface

This is where we will create a simple interface for our application. This code will only display the html table data and buttons. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  6. </head>
  7. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">How to Extract HTML Table Data</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <button class="btn btn-primary" onclick="extractTable();">Extract Data</button> <button class="btn btn-success" onclick="resetButton();">Reset</button>
  17. <br /><br />
  18. <div class="col-md-8" id="tbl">
  19. <table class="table table-bordered">
  20. <thead class="alert-info">
  21. <tr>
  22. <th>Names</th>
  23. </tr>
  24. </thead>
  25. <tbody id="table">
  26. <tr>
  27. <td>Tony Stark</td>
  28. </tr>
  29. <tr>
  30. <td>Steve Roger</td>
  31. </tr>
  32. <tr>
  33. <td>John Smith</td>
  34. </tr>
  35. <tr>
  36. <td>Claire Temple</td>
  37. </tr>
  38. </tbody>
  39. </table>
  40. </div>
  41. <div class="col-md-8" id="res">
  42. <h2>Extracted HTML Table Data</h2>
  43. <pre id="result"></pre>
  44. </div>
  45. </div>
  46. <script src="script.js"></script>
  47. </body>
  48. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will extract the table data when the button is clicked. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. document.getElementById('res').style.display="none";
  3. function extractTable(){
  4. let data=[];
  6. let table = document.getElementById('table');
  7. for (var i = 0, row; row = table.rows[i]; i++) {
  8. for (var j = 0, col; col = row.cells[j]; j++) {
  9. data.push(col.innerText);
  10. }
  11. }
  12. document.getElementById('tbl').style.display="none";
  13. document.getElementById('res').style.display="block";
  14. document.getElementById('result').innerHTML=JSON.stringify(data, null, 4);
  15. }
  17. function resetButton(){
  18. document.getElementById('res').style.display="none";
  19. document.getElementById('tbl').style.display="block";
  20. document.getElementById('result').innerHTML="";
  21. }

In the code above we just only create one method to extract the table data called extractTable(). This function uses nested loop to extract the table data from row to column. And then we set the extracted data a an array object.


The How to Extract HTML Table Data in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Extract HTML Table Data in JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment