Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read the first column values of a HTML table with jquery

I got a table:

<table id="ItemsTable" >​
    <tbody>
   <tr>
     <th>
       Number
     </th>
     <th>
       Number2
     </th>
    </tr>
  <tr>
    <td>32174711</td>     <td>32174714</td>
  </tr>
  <tr>
    <td>32174712</td>     <td>32174713</td>
  </tr>
</tbody>
</table>

I need the values 32174711 and 32174712 and every other value of the column number into an array or list, i'm using jquery 1.8.2.

like image 749
Milo Avatar asked Oct 05 '12 19:10

Milo


3 Answers

var arr = [];
$("#ItemsTable tr").each(function(){
    arr.push($(this).find("td:first").text()); //put elements into array
});

See this link for demo:

http://jsfiddle.net/CbCNQ/

like image 167
SUN Jiangong Avatar answered Nov 10 '22 16:11

SUN Jiangong


You can use map method:

var arr = $('#ItemsTable tr').find('td:first').map(function(){
     return $(this).text()
}).get()

http://jsfiddle.net/QsaU2/

From jQuery map() documentation:

Description: Pass each element in the current matched set through a function, producing a new jQuery object containing the return values. . As the return value is a jQuery-wrapped array, it's very common to get() the returned object to work with a basic array.

like image 12
undefined Avatar answered Nov 10 '22 16:11

undefined


// iterate over each row
$("#ItemsTable tbody tr").each(function(i) {
    // find the first td in the row
    var value = $(this).find("td:first").text();
    // display the value in console
    console.log(value);
});

http://jsfiddle.net/8aKuc/

like image 3
jrummell Avatar answered Nov 10 '22 16:11

jrummell