Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery - Select td value for all the tr with a class

Tags:

jquery

I have a table

<table>
       <tr class="PO1"></tr>
       <tr class="PO2"></tr>
       <tr class="PO3"></tr>
</table>

How can I loop through all tr with class "PO1" and get the value of each 'td' value?

$("table#id tr .PO1").each(function(i)
{
    // how to get the td values??
});
like image 510
Bhaskar Avatar asked May 13 '11 09:05

Bhaskar


People also ask

How to get selected tr value in jQuery?

btnSelect',function(){ // get the current row var currentRow=$(this). closest("tr"); var col1=currentRow. find("td:eq(0)"). text(); // get current row 1st TD value var col2=currentRow.

How to get value of td using jQuery?

var r=$("#testing":row2). text(); var r=$("#testing"). children("row2"). text();

How do I select a specific class in jQuery?

In jQuery, the class and ID selectors are the same as in CSS. If you want to select elements with a certain class, use a dot ( . ) and the class name. If you want to select elements with a certain ID, use the hash symbol ( # ) and the ID name.


2 Answers

var values = $('table tr.PO1 td').map(function(_, td) {
    return $(td).text();
}).get();

This would just create an array with the text contents from each td. Probably a better idea to use a map/object instead:

var values = $('table tr.PO1 td').map(function(index, td) {
    var ret = { };

    ret[ index ] = $(td).text();
    return ret;
}).get();
like image 113
jAndy Avatar answered Oct 19 '22 00:10

jAndy


notice : I removed a space before .PO1 because your tr has class P01

$("table#id tr.PO1").each(function(i)
{
    $(this).find("td").innerHTMl() //for example
});
like image 32
Grooveek Avatar answered Oct 18 '22 23:10

Grooveek