Reputation: 1486
How do I get every td in a row of a table?
HTML
<table>
<tr>
<td>
<td>Second td!</td>
</tr>
<tr>
<td>
<td>Another second td!</td>
</tr>
jQuery
$allrows = $("table tr");
//get all td in an index of $allrows
Upvotes: 1
Views: 59
Reputation: 845
You can use this selector to get every td
element that is inside a tr
which in turn is inside a table
.
$("table tr td");
If you want to restrict the selection to the specific hierarchy you
have (table - tr - td), I mean in case you have more tables nested, but you just want to target the parent td
elements. In this case you can use >
which targets only the immediate children:
$("table > tr> td");
If you dont care about a specific hierearchy, let say you are using
tbody
, or if you want to select all the td inside a table regardless
if you have nested tables you can use this:
$("table td");
EDIT:
If you want to keep the variableyou have with the list of rows ($allrows
) and select the td
elements from there, you can use then the children()
function. If you'd like, you could restrict the children selection to td
elements only:
$allrows.children('td').css("color", "red");
Upvotes: 1
Reputation: 43
Don't put a dot before the table tag, dots are used to select classes.
$("table tr");
Upvotes: 0
Reputation: 26258
Try jquery each()
like:
$("table tr td").each(function(){
// Do what ever you want here
});
Ex:
$(document).ready(function(){
$("table tr td").each(function(){
console.log($(this).html());
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table border="1">
<tr>
<td>One</td>
<td>Two</td>
</tr>
</table>
Upvotes: 2
Reputation: 9045
Remove the period from before the table like so:
$("table tr")
The period states a class so the way you have it you are looking for the class .table
row. Removing the period will look for the element table
.
Upvotes: 0