Skip to content

Instantly share code, notes, and snippets.

@Origame
Created October 25, 2016 09:06
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 Origame/da6adb549c7757cf8c25b76ffbbff673 to your computer and use it in GitHub Desktop.
Save Origame/da6adb549c7757cf8c25b76ffbbff673 to your computer and use it in GitHub Desktop.
Transpose rows & columns in HTML table (for mobile purposes mainly)
$("table").each(function() {
if ( !$(this).hasClass("reverted") ) {
var $this = $(this);
var newrows = [];
$this.find("tr").each(function(){
var i = 0;
$(this).find("td, th").each(function(){
i++;
if(newrows[i] === undefined) {
newrows[i] = $("<tr></tr>");
}
newrows[i].append($(this));
});
});
$this.find("tr").remove();
$.each(newrows, function(){
$this.append(this);
$this.addClass('reverted');
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment