Skip to content

Instantly share code, notes, and snippets.

@jpen365
Last active September 9, 2020 18:57
Show Gist options
  • Save jpen365/f34bbff7d9db99f912e1e75193071718 to your computer and use it in GitHub Desktop.
Save jpen365/f34bbff7d9db99f912e1e75193071718 to your computer and use it in GitHub Desktop.
Get table headings to use with responsive table layout
/* Credits:
This bit of code: Exis | exisweb.net/responsive-tables-in-wordpress
Original idea: Dudley Storey | codepen.io/dudleystorey/pen/Geprd */
var headertext = [];
var headers = document.querySelectorAll("thead");
var tablebody = document.querySelectorAll("tbody");
for (var i = 0; i < headers.length; i++) {
headertext[i]=[];
for (var j = 0, headrow; headrow = headers[i].rows[0].cells[j]; j++) {
var current = headrow;
headertext[i].push(current.textContent);
}
}
for (var h = 0, tbody; tbody = tablebody[h]; h++) {
for (var i = 0, row; row = tbody.rows[i]; i++) {
for (var j = 0, col; col = row.cells[j]; j++) {
col.setAttribute("data-th", headertext[h][j]);
}
}
}
@Elgameel
Copy link

Elgameel commented Apr 6, 2019

Hi @jpen365,

How to exclude certain tables?
For example how to exclude all tables with this class is-style-stripes this table:

<table class="wp-block-table aligncenter is-style-stripes">
<tbody>
<tr>
<th>Company</th><td>Amazon</td>
</tr>
<tr>
<th>Company</th><td>Google</td>
</tr>
</tbody>
</table>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment