Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: selecting each td in a tr

I need a way to interact with each td element in a tr.

To elaborate, I would like to access the first table row, then the first column, then the second column, etc. Then move onto the second row and repeat the process.

Something like this (pseudo-code):

for each row in table {   for each column in row   {     do cool things   } } 

jQuery:

$('#tblNewAttendees tr').each(function() {   alert('tr');   //Cool jquery magic that lets me iterate over all the td only in this row   $(magicSelector).each(function(){     alert('hi');   });  }); 

HTML:

<table>      <thead>           <th>someHeader</th>      </thead>      <tbody>           <tr>                <td>want to grab this first</td>                <td> this second </td>           </tr>           <tr>                <td>this third</td>                <td>this fourth</td>           </tr>      </tbody> </table> 
like image 570
gh9 Avatar asked Feb 14 '11 19:02

gh9


People also ask

How can get TD of TR in jquery?

jQuery(". dname"). find("tr td:eq(1)"). val();

How do I get the values of columns for a selected row using 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.


1 Answers

You can simply do the following inside your TR loop:

$(this).find('td').each (function() {   // do your cool stuff });                           
like image 59
Gregg Avatar answered Sep 25 '22 08:09

Gregg