<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Swap Cells</title>
<script type='text/javascript'>
//------------------------------------------------------------------
// Name: field()
// Role: Document element lookup using specified id attribute.
// Note: Returns a reference to the located element, or null
//------------------------------------------------------------------
function field( id ) {
var e = document.getElementById( id );
if ( !e ) {
alert( 'Specified element not found. id="' + id + '".' );
}
return e;
}
//------------------------------------------------------------------
// Name: Swap()
// Role: Exchange the contents of two table cells
//------------------------------------------------------------------
function swap( source, target ) {
var debug = field( 'debug' );
var td1 = field( source );
var td2 = field( target );
var cell1 = null;
var cell2 = null;
if ( td1 && td1.nodeName == 'TD' ) {
var cell1 = td1.firstChild;
} else {
alert( 'element not found, or wrong type. id="' + source + '"' );
return;
}
if ( td2 && td2.nodeName == 'TD' ) {
var cell2 = td2.firstChild;
} else {
alert( 'element not found, or wrong type. id="' + target + '"' );
return;
}
//----------------------------------------------------------------
// 1. Move source contents => temp
//----------------------------------------------------------------
var temp = field( 'temp' );
var kids = td1.childNodes.length;
for ( var i = 0; i < kids; i++ ) {
temp.appendChild( td1.firstChild );
}
//----------------------------------------------------------------
// 2. Move target contents => source
//----------------------------------------------------------------
var kids = td2.childNodes.length;
for ( var i = 0; i < kids; i++ ) {
td1.appendChild( td2.firstChild );
}
//----------------------------------------------------------------
// 3. Move temp contents => target
//----------------------------------------------------------------
var kids = temp.childNodes.length;
for ( var i = 0; i < kids; i++ ) {
td2.appendChild( temp.firstChild );
}
}
</script>
</head>
<body>
<table border='1' id='tbl'>
<tr>
<td id='source'><input type='text' value='Cell 1,1' size=8></td>
<td>1,2</td>
<td>1,3</td>
<td>1,4</td>
<td>1,5</td>
</tr>
<tr>
<td>2,1</td>
<td>2,2</td>
<td>2,3</td>
<td>2,4</td>
<td>2,5</td>
</tr>
<tr>
<td>3,1</td>
<td>3,2</td>
<td>3,3</td>
<td>3,4</td>
<td>3,5</td>
</tr>
<tr>
<td>4,1</td>
<td>4,2</td>
<td>4,3</td>
<td>4,4</td>
<td>4,5</td>
</tr>
<tr>
<td>5,1</td>
<td>5,2</td>
<td>5,3</td>
<td>5,4</td>
<td id='target'>
<table border='1'>
<tr>
<td>A</td>
<td>B</td>
</tr>
<tr>
<td>C</td>
<td>D</td>
</tr>
</table>
</td>
</tr>
</table>
<table border='1' style='display:none'><tr><td id='temp'></td></tr></table>
<input type='button' value='swap' onclick='swap("source","target")'>
<div id='debug'></div>
</body>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Random Cells</title>
<script type='text/javascript'>
function field( id ) {
var e = document.getElementById( id );
if ( !e ) {
alert( 'Specified element not found. id="' + id + '"' );
}
return e;
}
function Randomize() {
var before = [];
for ( var r = 0; r < 5; r++ ) {
for ( var c = 0; c < 5; c++ ) {
before[ before.length++ ] = ( r + 1 ) + ',' + ( c + 1 );
}
}
var after = [];
for ( var i = before.length; i > 0; i-- ) {
var j = Math.floor( Math.random() * ( i - 1 ) );
after[ after.length++ ] = before[ j ];
if ( j != before.length - 1 ) {
before[ j ] = before[ before.length - 1 ];
}
before.length--;
}
var tbl = document.getElementById( 'data' );
var tbody = document.getElementsByTagName( 'TBODY' )[ 0 ];
for ( r = i = 0; r < tbody.childNodes.length; r++ ) {
var row = tbody.childNodes[ r ];
if ( row.nodeName == 'TR' ) {
for ( c = 0; c < row.cells.length; c++ ) {
var cell = row.cells[ c ];
var text = document.createTextNode( after[ i++ ] );
row.childNodes[ c ].appendChild( text );
}
}
}
}
</script>
<body onload='Randomize()'>
<table id='data' border='1'>
<tbody>
<tr><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td><td></td></tr>
</tbody>
</table>
</body>
</html>
// +----------+----------+----------+----------+----------+
// | | | | | |
// | Cell 01 | Cell 02 | Cell 03 | Cell 04 | Cell 05 |
// | | | | | |
// +----------+----------+----------+----------+----------+
// | | | | | |
// | Cell 06 | Cell 07 | Cell 08 | Cell 09 | Cell 10 |
// | | | | | |
// +----------+----------+----------+----------+----------+
// | | | | | |
// | Cell 11 | Cell 12 | Cell 13 | Cell 14 | Cell 15 |
// | | | | | |
// +----------+----------+----------+----------+----------+
// | | | | | |
// | Cell 16 | Cell 17 | Cell 18 | Cell 19 | Cell 20 |
// | | | | | |
// +----------+----------+----------+----------+----------+
// | | | | | |
// | Cell 21 | Cell 22 | Cell 23 | Cell 24 | Cell 25 |
// | | | | | |
// +----------+----------+----------+----------+----------+
I am afraid that there is no simple way to swap columns. Propably only way to do that is to cycle trough TABLE rows and swap particular cells. So randomize first row and apply the same pattern for the rest of the TABLE.