JavaScript - Simple Extract Table Data As Array

In this tutorial we will create a Simple Extract Table Data As Array using JavaScript. This code can extract all the data in the table when user click the extract button. The code use onclick() to call a specific method that extract the data in the table by using for() loop to break down the row, then compress the fragment using dot notation push() in order to generate a readable array format. Feel free to modify and apply it in your system, this is a user-friendly kind of program We will be using JavaScript as a server-side scripting language because It gives a greater control of your web page and extend its capability in a modern way approach. It is written in HTML or as an external sourcing to add some necessary features in your website.

Getting started:

First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
  1. <!DOCTYPE html>
  2. <!DOCTYPE html>
  3. <html lang="en">
  4.         <head>
  5.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
  6.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  7.         </head>
  8.         <nav class="navbar navbar-default">
  9.                 <div class="container-fluid">
  10.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11.                 </div>
  12.         </nav>
  13.         <div class="col-md-3"></div>
  14.         <div class="col-md-6 well">
  15.                 <h3 class="text-primary">JavaScript - Simple Extract Table Data As Array</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <button class="btn btn-primary" onclick="extractTable();">Extract</button>
  18.                 <br /><br />
  19.                 <div class="col-md-6">
  20.                         <table class="table table-bordered">
  21.                                 <thead class="alert-info">
  22.                                         <tr>
  23.                                                 <th>Full Names</th>
  24.                                         </tr>
  25.                                 </thead>
  26.                                 <tbody id="table">
  27.                                         <tr>
  28.                                                 <td>John Smith</td>
  29.                                         </tr>
  30.                                         <tr>
  31.                                                 <td>Claire Temple</td>
  32.                                         </tr>
  33.                                         <tr>
  34.                                                 <td>Steve Rogers</td>
  35.                                         </tr>
  36.                                 </tbody>
  37.                         </table>
  38.                 </div>
  39.                 <div class="col-md-6">
  40.                         <pre id="result"></pre>
  41.                 </div>
  42.         </div>
  43. <script src="js/script.js"></script>   
  44. </body>
  45. </html>

Creating the Script

This code contains the script of the application. 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, then save it as script.js inside the js folder.
  1. function extractTable(){  
  2.     var data=[];
  3.        
  4.         var table = document.getElementById('table');
  5.     for (var i = 0, row; row = table.rows[i]; i++) {
  6.                 for (var j = 0, col; col = row.cells[j]; j++) {
  7.                         data.push(col.innerText);
  8.                 }
  9.     }
  10.        
  11.         document.getElementById('result').innerHTML=JSON.stringify(data, null, 4);
  12. }
There you have it we successfully created a Simple Extract Table Data As Array using 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!

Add new comment