I am trying to retrieve all rows from within a <tbody>
section of a table, but am unsure of the syntax for doing so. I've included a dummy table extract below and my latest attempt for achieving the task with jQuery!
Table extract:
<tbody>
<tr>
<th id="emergency" colspan="2">Emergency</th>
</tr>
<tr>
<td>Emergency data</td>
<td>Emergency data</td>
</tr>
<tr>
<td>Emergency data</td>
<td>Emergency data</td>
</tr>
</tbody>
<tbody>
<tr>
<th id="urgent" colspan="2">Urgent</th>
</tr>
<tr>
<td>Urgent Data</td>
<td>Urgent Data</td>
</tr>
<tr>
<td>Urgent Data</td>
<td>Urgent Data</td>
</tr>
</tbody>
jQuery code:
var emergencyRows = $table.find('#emergency').children().get();
You can use the below if you know the table id.
var trs = $("#tableid").find("tbody>tr");
My suggestions is to place the ID attributes on the tbody rather than the first row of each one.
HTML
<table>
<tbody id="emergency">
<tr>
<th colspan="2">Emergency</th>
</tr>
<tr>
<td>Emergency data</td>
<td>Emergency data</td>
</tr>
<tr>
<td>Emergency data</td>
<td>Emergency data</td>
</tr>
</tbody>
<tbody id="urgent">
<tr>
<th colspan="2">Urgent</th>
</tr>
<tr>
<td>Urgent Data</td>
<td>Urgent Data</td>
</tr>
<tr>
<td>Urgent Data</td>
<td>Urgent Data</td>
</tr>
</tbody>
</table>
jQuery
var emergencyRows = $("tbody#emergency").find("tr:gt(0)");
var urgentRows = $("tbody#urgent").find("tr:gt(0)");
The jQuery snippet will get all the respective rows with the exception of the first rows.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With