Reputation: 8141
I have a table and I have a jQuery object that represents one of the TD in the table.
I want to be able to loop through all the rows in the table starting at the next row from the row my jQuery'd TD is in to the end.
So if I have a table like this:
<table>
<tr><td>A</td><td>B</td><td>C</td></tr>
<tr><td>D</td><td>E</td><td>F</td></tr>
<tr><td>G</td><td>H</td><td>I</td></tr>
<tr><td>J</td><td>K</td><td>L</td></tr>
<tr><td>M</td><td>N</td><td>O</td></tr>
<tr><td>P</td><td>Q</td><td>R</td></tr>
</table>
And suppose I have a jQuery object representing the td with an H in it:
var theTD = $(...the H cell...);
In this case, I would want to loop through the last 3 rows.
So is there a nice succint piece of jQuery that would give me the equivalent of:
theTD.nextRow_to_lastRow.each(...
Upvotes: 4
Views: 3160
Reputation: 8141
With the help of ilan, I got there in the end:
theTD.parent().find("~ tr").each(
Other replys will do the job, but the above is what I was looking for
Upvotes: 0
Reputation: 2248
You can also use a for-loop which targets only the last three rows:
var table = document.getElementById('table'),
row = table.querySelectorAll('tr');
for(i = 0; i < row.length; i++) {
if (i >= 3) {
console.log(row[i]);
}
}
Upvotes: 0
Reputation: 20250
I think you're after something like this:
$('td').filter(function() {
return $(this).text() == 'H';
}).parent().nextAll().each(function() {
console.log(this);
});
Which would give you the rows:
<tr><td>J</td><td>K</td><td>L</td></tr>
<tr><td>M</td><td>N</td><td>O</td></tr>
<tr><td>P</td><td>Q</td><td>R</td></tr>
Upvotes: 1
Reputation: 32581
Something like this?
var td = $('td:contains("H")');
var tr = $('tr').length;
for(i= td.parent().index()+1; i < tr;i++){
$.each($('tr:eq('+i+') td'),function(){
console.log($(this).text());
});
}
Check demo in the console
Upvotes: 1
Reputation: 3374
Your situation is quite complicated and I couldn't understand it completely, however; hopefully this helps: by using
var rows = $("#tableid tr td");
you will have all the cell. now you can iterate through each one and check the value or text. foreach in jquery:
$(rows).each(function( index ) {
console.log( index + ": " + $(this).text() );
});
Upvotes: 0
Reputation: 740
var all_td_in_a_table = $("#table-id td")
and then loop trough it with for or foreach and search for h/div/text or what you want
Upvotes: 0