artdir
07-24-2006, 06:41 PM
I am new in JS and is faced with a problem of randomization of the table rows, without including content of each table row inside JS (I haven't seen this attempted before).
So far I have the following code (see below) and the problem is that I don't know how to extract data from <tr> and create an array out of them, so then I could randomize them.
Any kind of tips, pointers, help of any kind will be really truly well appreciated.
CODE:
<html>
<head>
<title>Random Table Example</title>
<script language="javascript" type="text/javascript">
function randomize(id){
if(document.getElementsByTagName){
var table = document.getElementById(id);
var rows = table.getElementsByTagName("tr");
alert ("Table Raws: " + rows.length);
var test_it = new Array (rows.length);
// Assign nothing to each test_it element so rows will not repeat upon being randomized
for (i=0; i < rows.length; i++){
test_it[i] = "";
}
for (i=0; i < rows.length; i++){
// => rows[i] = get.childnode; // How do I get content of everything inside <tr>? and create an array out of it?
}
for(i = 0; i < rows.length; i++){
rndm = Math.round(Math.random() * rows.length)
if (test_it[rndm] == "")
{
test_it[rndm] = rows[rndm] // content of the array should be here
hits += 1
document.write('<table width="500" align="center"><tr>' + rows[rndm] + '<\/tr><\/table>');
}
}}}
</script>
</head>
<body onLoad="randomize('randomtable')">
<table width="560" border="0" cellpadding="0" cellspacing="2" id="randomtable">
<tr>
<td width="227">Row 1 - Black </td>
<td width="55" bgcolor="#000000"> </td>
<td width="278"><div align="center">Black</div></td>
</tr>
<tr>
<td>Row 2 - Brown </td>
<td bgcolor="#D47F55"> </td>
<td><div align="center">Brown</div></td>
</tr>
<tr>
<td>Row 3 - Green </td>
<td bgcolor="#009F00"> </td>
<td><div align="center">Green</div></td>
</tr>
<tr>
<td>Row 4 - Red </td>
<td bgcolor="#AA0000"> </td>
<td><div align="center">Red</div></td>
</tr>
<tr>
<td>Row 5 - Blue </td>
<td bgcolor="#009FFF"> </td>
<td><div align="center">Blue</div></td>
</tr>
<tr>
<td>Row 6 - Orange </td>
<td bgcolor="#FF7F55"> </td>
<td><div align="center">Orange</div></td>
</tr>
<tr>
<td>Row 7 - Purple </td>
<td bgcolor="#AA00FF"> </td>
<td><div align="center">Purple</div></td>
</tr>
<tr>
<td>Row 8 - Pink </td>
<td bgcolor="#FF00FF"> </td>
<td><div align="center">Pink</div></td>
</tr>
</table>
</body>
</html>
So far I have the following code (see below) and the problem is that I don't know how to extract data from <tr> and create an array out of them, so then I could randomize them.
Any kind of tips, pointers, help of any kind will be really truly well appreciated.
CODE:
<html>
<head>
<title>Random Table Example</title>
<script language="javascript" type="text/javascript">
function randomize(id){
if(document.getElementsByTagName){
var table = document.getElementById(id);
var rows = table.getElementsByTagName("tr");
alert ("Table Raws: " + rows.length);
var test_it = new Array (rows.length);
// Assign nothing to each test_it element so rows will not repeat upon being randomized
for (i=0; i < rows.length; i++){
test_it[i] = "";
}
for (i=0; i < rows.length; i++){
// => rows[i] = get.childnode; // How do I get content of everything inside <tr>? and create an array out of it?
}
for(i = 0; i < rows.length; i++){
rndm = Math.round(Math.random() * rows.length)
if (test_it[rndm] == "")
{
test_it[rndm] = rows[rndm] // content of the array should be here
hits += 1
document.write('<table width="500" align="center"><tr>' + rows[rndm] + '<\/tr><\/table>');
}
}}}
</script>
</head>
<body onLoad="randomize('randomtable')">
<table width="560" border="0" cellpadding="0" cellspacing="2" id="randomtable">
<tr>
<td width="227">Row 1 - Black </td>
<td width="55" bgcolor="#000000"> </td>
<td width="278"><div align="center">Black</div></td>
</tr>
<tr>
<td>Row 2 - Brown </td>
<td bgcolor="#D47F55"> </td>
<td><div align="center">Brown</div></td>
</tr>
<tr>
<td>Row 3 - Green </td>
<td bgcolor="#009F00"> </td>
<td><div align="center">Green</div></td>
</tr>
<tr>
<td>Row 4 - Red </td>
<td bgcolor="#AA0000"> </td>
<td><div align="center">Red</div></td>
</tr>
<tr>
<td>Row 5 - Blue </td>
<td bgcolor="#009FFF"> </td>
<td><div align="center">Blue</div></td>
</tr>
<tr>
<td>Row 6 - Orange </td>
<td bgcolor="#FF7F55"> </td>
<td><div align="center">Orange</div></td>
</tr>
<tr>
<td>Row 7 - Purple </td>
<td bgcolor="#AA00FF"> </td>
<td><div align="center">Purple</div></td>
</tr>
<tr>
<td>Row 8 - Pink </td>
<td bgcolor="#FF00FF"> </td>
<td><div align="center">Pink</div></td>
</tr>
</table>
</body>
</html>