Okay, I have a table that looks like so (more or less)
CODE
Age | Name
14 | John
20 | Adam
Where Age and Name are table heads. I am trying to get the contents of the table rows (excluding the heads) into an array based on column. So array 1 would be ("14", "20") the ages, and array 2 would be ("John", "Adam")
I have tried using this as my JS (I know it has errors and is overall incorrect)
CODE
function makeSortable(obj){
obj.order = new Array();
for(var x=0; x<obj.rows.length; x++){
if(x == 0){
for(var y=0; y<obj.rows[0].childNodes.length; y++){
obj.rows[0].childNodes[y].onclick = function(){
alert(this.innerHTML+" was clicked");
}
obj.order.push(y);
}
}
else{
for(var y=0; y<obj.rows[x].childNodes.length; y++){
obj.order[y].push(obj.rows[x].childNodes[y].innerHTML);
}
alert(obj.order);
}
}
}
function init(){
tables = document.getElementsByTagName("TABLE");
for(var i=0; i<tables.length; i++){
if(tables[i].className == 'sortable'){
makeSortable(tables[i]);
}
}
}
And I have also tried this (Whcih I am a bit confused as to whyu it isn't working but it is (no errors are outputted with this one, it just doesn't work).
CODE
function makeSortable(obj){
this.order = new Object();
this.order.colnames = new Array("T");
alert(this);
for(var x=0; x<obj.rows.length; x++){
if(x == 0){
for(var y=0; y<obj.rows[0].childNodes.length; y++){
obj.rows[0].childNodes[y].onclick = function(){
alert(this.innerHTML+" was clicked");
}
alert(this.order);
a = new Array();
this.order.colnames.push(a);
}
}
else{
for(var y=0; y<obj.rows[x].childNodes.length; y++){
this.order.colnames[y].a.push(obj.rows[x].childNodes[y]);
}
alert(this.order);
}
}
}
vat tbl_ = new Array();
function init(){
tables = document.getElementsByTagName("TABLE");
for(var i=0; i<tables.length; i++){
if(tables[i].className == 'sortable'){
tbl_[i] = new makeSortable(tables[i]);
}
}
}
Error Output -
Object doesn't support this property or methodTalking about tis line (first off) -
obj.order[y].push(obj.rows[x].childNodes[y].innerHTML);Here is what my HTML looks like at the moment:
CODE
<body onload="init();">
<table class="sortable">
<tr>
<th id="col_1" name="numeric">
Age
</th>
<th id="col_2" name="alpha">
Name
</th>
</tr>
<tr>
<td>
14
</td>
<td>
John
</td>
</tr>
<tr>
<td>
20
</td>
<td>
Adam
</td>
</tr>
</table>
All I need to get done (at the moment) is dividing the table columns up into separate arrays (so I can sort them and everything later and then reorder the table without returning to the server side script after the page has loaded).
I know that there are other scripts out there that do this already but I am just trying to make it from scratch because I have ideas on interesting (and useful) ways of changing it to be better than what is currently out there.
I also understand that the code is a little flawed but I haven't done much difficult stuff with JS pretty much all summer so now that I am getting back into it I begin to realize how much I have lost.
Thanks in advance.