JavaScript - Simple Extract Table Data As Array

Submitted by: 
Language: 
Visitors have accessed this post 135 times.

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="<a href="https://sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">JavaScript - Simple Extract Table Data As Array</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <button class="btn btn-primary" onclick="extractTable();">Extract</button>
  19. <br /><br />
  20. <div class="col-md-6">
  21. <table class="table table-bordered">
  22. <thead class="alert-info">
  23. <tr>
  24. <th>Full Names</th>
  25. </tr>
  26. </thead>
  27. <tbody id="table">
  28. <tr>
  29. <td>John Smith</td>
  30. </tr>
  31. <tr>
  32. <td>Claire Temple</td>
  33. </tr>
  34. <tr>
  35. <td>Steve Rogers</td>
  36. </tr>
  37. </tbody>
  38. </table>
  39. </div>
  40. <div class="col-md-6">
  41. <pre id="result"></pre>
  42. </div>
  43. </div>
  44. <script src="js/script.js"></script>
  45. </body>
  46. </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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.