Skip to content

Instantly share code, notes, and snippets.

@pgaertig
Created April 13, 2012 13:48
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save pgaertig/2376975 to your computer and use it in GitHub Desktop.
Save pgaertig/2376975 to your computer and use it in GitHub Desktop.
Transpose HTML table using jQuery
$(function() {
var t = $('#thetable tbody').eq(0);
var r = t.find('tr');
var cols= r.length;
var rows= r.eq(0).find('td').length;
var cell, next, tem, i = 0;
var tb= $('<tbody></tbody>');
while(i<rows){
cell= 0;
tem= $('<tr></tr>');
while(cell<cols){
next= r.eq(cell++).find('td').eq(0);
tem.append(next);
}
tb.append(tem);
++i;
}
$('#thetable').append(tb);
$('#thetable').show();
}
@envitraux
Copy link

Very nice. Thanks

@waqashsn
Copy link

Can you modify this script so that it runs when a button is clicked?

@wei-li-2016
Copy link

This code works

@jishakannan
Copy link

It works great. But I have other requirement. I have multiple tables on my pages and these tables are dynamicaly generated. And all of these tables have headers. I want to transponse the table including the headers. And want to maintain the row styles too. I would be great if you modify the js to meet the above requirement. Thanks

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