1

I have this table which contains essential information about my book cart

<table style="width:100%">
  <tr>
    <th>School No</th>
    <th>Resource Type</th> 
    <th>Resource No.</th>
    <th>Resource Title</th>
    <th>Quantity</th>
  </tr>
  <tr>
    <td>201912621</td>
    <td>Book</td> 
    <td>099918723</td>
    <td>Sample Title</td>
    <td>10</td>
  </tr>
</table>

To make it short how can I read each row in my table and ouput it like this in jquery.

var row1= $("#SchoolNo").val();
Corvus
  • 27
  • 5

1 Answers1

0

function table2array(){
  var out=[];
  $('table tr').each(function(){
    var el=[]
    $(this).children('td').each(function(){
     el.push($(this).text());
    });
     if(el.length){
      out.push(el);
     }
  });
  return out;
}

$('#btn_get_all').click(function(){
 var my_arr=table2array();
 console.log(my_arr);
});

$('#btn_get_one').click(function(){
 var my_arr=table2array();
 alert(my_arr[1][3]);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>






<table style="width:100%" border="1">
  <tr>
    <th>School No</th>
    <th>Resource Type</th>
    <th>Resource No.</th>
    <th>Resource Title</th>
    <th>Quantity</th>
  </tr>
  <tr>
    <td>201912621</td>
    <td>Book</td>
    <td>099918723</td>
    <td>Sample Title</td>
    <td>10</td>
  </tr>
  <tr>
    <td>201912456</td>
    <td>Cd</td>
    <td>099918745</td>
    <td>Sample Title 2</td>
    <td>11</td>
  </tr>
</table>


<input type="button" id="btn_get_all" value="get all TABLE content"></br>
<input type="button" id="btn_get_one" value="get SELL(1,3)">
mscdeveloper
  • 2,749
  • 1
  • 10
  • 17