Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
180
rated 0 times [  184] [ 4]  / answers: 1 / hits: 99585  / 13 Years ago, thu, june 9, 2011, 12:00:00

I want to transpose the rows and columns in an HTML table, i.e. Rows as Columns, Columns as Rows.



In what way I can do it?



Example :



1 4 7  
2 5 8
3 6 9


as



1 2 3  
4 5 6
7 8 9

More From » html-table

 Answers
10

http://jsfiddle.net/CsgK9/2/



html:



<table>
<tr>
<td>1</td>
<td>4</td>
<td>7</td>
</tr>
<tr>
<td>2</td>
<td>5</td>
<td>8</td>
</tr>
<tr>
<td>3</td>
<td>6</td>
<td>9</td>
</tr>
</table>


<p><a href=#>Do it.</a></p>


js:



$(a).click(function(){
$(table).each(function() {
var $this = $(this);
var newrows = [];
$this.find(tr).each(function(){
var i = 0;
$(this).find(td).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);
});
});

return false;
});

[#91783] Wednesday, June 8, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jocelynkarsynr

Total Points: 472
Total Questions: 98
Total Answers: 96

Location: Macau
Member since Mon, Nov 16, 2020
4 Years ago
jocelynkarsynr questions
Tue, Feb 8, 22, 00:00, 2 Years ago
Sat, Jul 11, 20, 00:00, 4 Years ago
Sun, May 10, 20, 00:00, 4 Years ago
Sat, Jan 18, 20, 00:00, 4 Years ago
;