Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
27
rated 0 times [  30] [ 3]  / answers: 1 / hits: 19600  / 10 Years ago, wed, july 23, 2014, 12:00:00

Is there a way (I don't know the right word for it) to twist a table with jQuery? With a function or something?



For example I have an table like this:



<table>
<tr>
<th></th>
<th></th>
<th></th>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</table>


Table1



And I want it like this:



<table>
<tr>
<th></th>
<td></td>
</tr>
<tr>
<th></th>
<td></td>
</tr>
<tr>
<th></th>
<td></td>
</tr>
</table>


enter


More From » jquery

 Answers
95

try this code



<style>
td { padding:5px; border:1px solid #ccc;}
</style>
<script>
$(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;
});

</script>
<table>
<tr>
<td>Heading1</td>
<td>Heading2</td>
<td>Heading3</td>
</tr>

<tr>
<td>data</td>
<td>data</td>
<td>data</td>
</tr>
</table>


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


don't forgot to link necessary (jquery1.6) file and appropriate html tags.



try give link http://jsfiddle.net/CsgK9/2/



see this link for original view
https://stackoverflow.com/a/6298066


[#70075] Tuesday, July 22, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dantel

Total Points: 7
Total Questions: 102
Total Answers: 97

Location: Saint Lucia
Member since Sat, Jun 6, 2020
4 Years ago
;