I have a table that looks something like this, where most of the rows are not displayed:
<table id="myTable" class="tablesorter">
<thead>
<tr>
<th>header1</th>
<th>header2</th>
...
</tr>
</thead>
<tbody>
<tr style="display:none;">
<td>content1</td>
<td>content2</td>
...
</tr>
<tr style="display:none;">
<td>content1</td>
<td>content2</td>
...
</tr>
<tr>
<td>content1</td>
<td>content2</td>
...
</tr>
...
</tbody>
</table>
I am trying to access all of the visible rows in the table, and concatenate each cells' value to a string that will be output into CSV. I have some jQuery code but it doesn't get very far at the moment:
<input type="button" value="Export" id="csv-export" />
<script type="text/javascript">
$(function () {
$("#csv-export").click(function () {
var csvStr = "";
$("#myTable tr:visible td").each(function () { //for each td in each visible row
if ($(this).has("a") == false) { //if the td does not contain a link
csvStr += $(this).html(); //Append the td's html
}
});
$.get('@Url.Action("CSVExport")', { csv: csvStr });
});
});
</script>
The line $("#myTable tr:visible td").each(function () {
is failing. Any help here? I'm a bit of an amateur at writing this type of code. I want this code to select all tr
elements that are not set to display: none
and then for each of these, select and access all td
elements.