Skip to content

Instantly share code, notes, and snippets.

@Archie22is
Created September 28, 2022 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Archie22is/34298569cc2804e66d00c02cc1c9bc20 to your computer and use it in GitHub Desktop.
Save Archie22is/34298569cc2804e66d00c02cc1c9bc20 to your computer and use it in GitHub Desktop.
Wrap and unwrap tables using jQuery based on screen width
<!-- HTML -->
<table>
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
<td>Cell 3</td>
</tr>
<tr>
<td>Cell 4</td>
<td>Cell 5</td>
<td>Cell 6</td>
</tr>
</table>
<!-- CSS -->
/* Responsive tables class */
.scrollable-table {
overflow-x:scroll;
}
<!-- JS -->
/**
* Responsive tables
*
*/
if( $(window).width() > 500 ){
//remove wrapper around tables
if ( $('table').parent().is( "div.scrollable-table" ) ) {
pTags.unwrap();
}
console.log("Responsive tables disabled");
} else {
//add table wrapper
$('table').wrap('<div class="scrollable-table"></div>');
console.log("Responsive tables enabled");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment