Read Table Header And Row Data Using JQuery

This blog will show you, how you can read all the values of HTML table control, using jQuery. Here is HTML table whose row value, we will read, using jQuery. Thus, in this article, I will show two ways to read the rows value. One is to read all the table value and other to read a specific row value. 
  1. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
  2. <table id="tblArticles" border="1px" cellpadding="2" cellspacing="2">  
  3. <caption style="text-align:left;font-weight:bold;">Article List:</caption>  
  4.  <thead style="background-color:Lime;">  
  5.   <tr>  
  6.      <th>Article Code</th>  
  7.      <th>Article Title</th>  
  8.      <th>Published Date</th>  
  9.      <th>No. of Views</th>  
  10.   </tr>  
  11.  </thead>  
  12.  <tbody>  
  13.   <tr>  
  14.      <td>0001</td>  
  15.      <td>JQuery to read HTML Table Contents</td>  
  16.      <td>Jan, 2015</td>  
  17.      <td>5899</td>  
  18.   </tr>  
  19.   <tr>  
  20.      <td>0002</td>  
  21.      <td>JQuery to read HTM Table Cell Value</td>  
  22.      <td>Feb, 2015</td>  
  23.      <td>4567</td>  
  24.   </tr>  
  25.   <tr>  
  26.      <td>0003</td>  
  27.      <td>JQuery to read HTM Table Row Value</td>  
  28.      <td>Mar, 2015</td>  
  29.      <td>3985</td>  
  30.   </tr>  
  31.   <tr>  
  32.      <td>0004</td>  
  33.      <td>JQuery to read HTM Table Column Value</td>  
  34.      <td>Apr, 2015</td>  
  35.      <td>2354</td>  
  36.   </tr>  
  37.  </tbody>  
  38. </table>  
Now, we will read all the row values by jQuery. For this, we will add a button control. On click of button control, we will display the detail.
  1. <input id="btn_read_HTML_Table" type="button" value="Read HTML Table Using JQuery/Javascript" />   
Read all the data of the table.
  1. <script type="text/javascript">  
  2.         $(document).ready(function () {  
  3.             $('#btn_read_HTML_Table').click(function () {  
  4.                 var html_table_data = "";  
  5.                 var bRowStarted = true;  
  6.                 $('#tblArticles tbody>tr').each(function () {  
  7.                     $('td'this).each(function () {  
  8.                         if (html_table_data.length == 0 || bRowStarted == true) {  
  9.                             html_table_data += $(this).text();  
  10.                             bRowStarted = false;  
  11.                         }  
  12.                         else  
  13.                             html_table_data += " | " + $(this).text();  
  14.                     });  
  15.                     html_table_data += "\n";  
  16.                     bRowStarted = true;  
  17.                 });  
  18.    
  19.                 alert(html_table_data);  
  20.             });  
  21.         });  
  22. </script>  
Now, check the output.
 
output
 
Now, code to read the header value is given below- 
  1. $('#btn_read_HTML_Table').click(function () {  
  2.     var html_table_data = "";  
  3.     var bRowStarted = true;  
  4.     $('#tblArticles thead>tr').each(function () {  
  5.         $('th'this).each(function () {  
  6.             if (html_table_data.length == 0 || bRowStarted == true) {  
  7.                 html_table_data += $(this).text();  
  8.                 bRowStarted = false;  
  9.             }  
  10.             else  
  11.                 html_table_data += " | " + $(this).text();  
  12.         });  
  13.         html_table_data += "\n";  
  14.         bRowStarted = true;  
  15.     });  
  16.    
  17.     alert(html_table_data);  
  18. });   
Now, check the output.
 
output