views:

49

answers:

4

I am in the the each() loop of a given <tr> on my page. How do I loop through all the <th> within $(this) i.e. within the current <tr>:

$('#my_table > tbody > tr').each(function() {
   // how to loop through all the <th>'s inside the current tr?
});
+1  A: 
$('#my_table > tbody > tr').each(function() {
   $('th', this).each( ... );
});
KennyTM
A: 
$('#my_table > tbody > tr').each(function() {
  $(this).filter('th').each(function(){
   //your code here
   });
});

but it can be done more concisely if you don't need to perform any operations on the tr or can chain them.

Mike
`filter` will remove any elements from the _current_ selector that aren't `th`, rather than get children that are `th`
bdukes
A: 
$(this).find('th').each(...)

OR

$('th', this).each(...)
bdukes
+1  A: 

you could do this in the original loop:

$('#my_table > tbody > tr > th').each(function() {

but if you can't do that for some reason, like there is other code in the loop, you can use this:

$('th',this).each(function() {
GSto