Milo
Milo

Reputation: 318

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.

Upvotes: 9

Views: 37236

Answers (5)

Ram
Ram

Reputation: 144689

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.

Upvotes: 12

SUN Jiangong
SUN Jiangong

Reputation: 5312

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/

Upvotes: 16

jrummell
jrummell

Reputation: 43077

// 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/

Upvotes: 4

Shmiddty
Shmiddty

Reputation: 13967

http://jsfiddle.net/Shmiddty/zAChf/

var items = $.map($("#ItemsTable td:first-child"), function(ele){
    return $(ele).text();
});

console.log(items);​

Upvotes: 0

Huangism
Huangism

Reputation: 16438

well from what you have, you can use first-child

var td_content = $('#ItemsTable tr td:first-child').text()
// loop the value into an array or list

Upvotes: 2

Related Questions