Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting text from td cells with jQuery

I have this code in jQuery:

children('table').children('tbody').children('tr').children('td') 

Which gets all table cells for each row. My question is: how can I get the text value in each cell in each row?

Should I use .each() to loop trough all children('td')? How can I get the text value of each td?

like image 718
Vlad Avatar asked Nov 30 '11 12:11

Vlad


People also ask

How can get TD table row value in jQuery?

$('#mytable tr'). each(function() { var customerId = $(this). find("td:first"). html(); });

How can get TD value using ID in jQuery?

$(document). ready(function(){ var r=$("#testing":row2). val(); alert(r); });

How do I find the value of TD tags?

jQuery: code to get TD text value on button click.text() method we get the TD value (table cell value). So our code to get table td text value looks like as written below. $(document).


2 Answers

First of all, your selector is overkill. I suggest using a class or ID selector like my example below. Once you've corrected your selector, simply use jQuery's .each() to iterate through the collection:

ID Selector:

$('#mytable td').each(function() {     var cellText = $(this).html();     }); 

Class Selector:

$('.myTableClass td').each(function() {     var cellText = $(this).html();     }); 

Additional Information:

Take a look at jQuery's selector docs.

like image 185
James Hill Avatar answered Sep 29 '22 22:09

James Hill


You can use .map: http://jsfiddle.net/9ndcL/1/.

// array of text of each td  var texts = $("td").map(function() {     return $(this).text(); }); 
like image 39
pimvdb Avatar answered Sep 29 '22 21:09

pimvdb