Mel Sy Gallosa Mel Sy Gallosa - 1 month ago 12
HTML Question

Get element in jQuery

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

Answer Source
  • 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");