9

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.

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

Comments

12

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.

10 Comments

@Huangism Yes, it's needed, your selector only returns the first td of the first tr. jsfiddle.net/EB4rN
@Shmiddty That's an another option, Why do you think get is not necessary? There is a difference between $.map() and map(). map returns a jQuery object.
@undefined: get or no get, both fiddles return the same array (["32174711", "32174712"]) in the console as far as I can see.
@FrançoisWahl I can see all methods and properties of a jQuery object, one is a jQuery object and another one is actual array.
@undefined: Ahhhhhh.. now I see. Sorry for not seeing it! Very good :) when not using get the result still is a jQuery object. I can call jQuery methods on it such as console.log(arr.eq(0)) for example but can't do that when using get(). Very nice. You are absolutely correct in that case. Even though the results look the same in the console, without get it still is a jQuery wrapper. Nice one. Off course, if that is what the user wants then leaving get() away is right I suppose but if you want the underlying values without being a jQuery object get() is needed.
|
4
// 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/

3 Comments

Isn't that a lot of internal traversing? First .each then for each row another jQuery wrapper on this plus the find. Not sure how map internally works but using .find only ones sounds "cheaper" than using it each time inside the loop. +1 anyway as it does give you the individual values.
This answer is probably the least efficient. Just sayin.
I can't speak on the internals of map, but I imagine it does something similar.
2

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 

Comments

0

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.