Click to See Complete Forum and Search --> : referencing a table
rogers
09-22-2003, 04:15 PM
Hello,
If I have 2 forms on a page, how do I refer to the table inside the second form when using document.getElementById(...) ? What goes inside the parenthesis?
Thanks
AdamBrill
09-22-2003, 04:32 PM
You put the ID of the table inside there... If your code looks like this:
<form name="form1">
<table>
<tr>
<td>
test
</td>
</tr>
</table>
</form>
<form name="form2">
<table id="secondtable">
<tr>
<td>
test2
</td>
</tr>
</table>
</form>
then you would get the second table like this:
element = document.getElementById('secondtable');
rogers
09-22-2003, 04:42 PM
Adam,
I tried your suggestion and wanted to print out the number of rows. The table has 2 rows, but the print out indicated zero rows. Here is the code I used:
var numrows = document.getElementById("tasktable").rows.length;
document.write("numrows = " + numrows);
Can you tell me why I am not getting the correct number of rows in the table?
Thanks
AdamBrill
09-22-2003, 04:48 PM
Here is a complete example... The second table has one row and it will alert and tell you:<html>
<head>
<title>Untitled</title>
</head>
<body>
<form name="form1">
<table>
<tr>
<td>
test
</td>
</tr>
</table>
</form>
<form name="form2">
<table id="secondtable">
<tr>
<td>
test2
</td>
</tr>
</table>
</form>
<script type="text/javascript">
alert(document.getElementById('secondtable').rows.length);
</script>
</body>
</html>
rogers
09-22-2003, 05:35 PM
Adam,
That worked fine :)
Now I want to go through the rows in the table which contains 6 columns, and print out the 5th column in each row. So here is what I tried but did not work:
function sum_new(f)
{
var rstr;
var tblrows=document.getElementById("tasktable").getElementsByTagName("TR");
for(var i=2; i<tblrows.length - 1; i++)
{
rstr = tblrows[i].childNodes[4].value + " ");
alert(rstr);
}
}
I started from the 2nd row, because the first row has headings.
Please let me know what I did wrong.
Thanks
AdamBrill
09-22-2003, 06:25 PM
Try changing this line:
for(var i=2; i<tblrows.length - 1; i++)
to this:
for(var i=1; i<tblrows.length; i++)
I think that should work better... Let me know. ;)
rogers
09-23-2003, 08:34 AM
Hi Adam,
Sorry, the heading takes up 2 rows. So I started on the 3rd row, so I used index of 2. Then the last row is not to be included so I used tblrows.length - 1 in the for loop.
Since the for loop did not work, I tried to do the following before the for loop:
alert(tblrows[2].childNodes[4].innerHTML);
That displayed the html for that column ( I have an onKeyUp for that column) so this tells me that I am pointing to the correct column.
But how do I get the numeric value out of that cell? SO I tried this:
tblrows[2].childNodes[4].value
The alert box said "undefined". How can that be undefined?
The number in that cell is 2877488.74. Is that number too large to handle?
Please let me know if you have any suggestions.
rogers
09-23-2003, 08:35 AM
Adam,
I made a mistake and created a new thread instead of responding to this thread.
Please see the new thread I created.
Thanks
rogers
09-23-2003, 09:05 AM
I tried it on a column which had the value of 0.00, but I still got "undefined" when trying to access the value.
Any suggestions woould be helpful
Thank you.
rogers
09-23-2003, 09:26 AM
Here is an example of the table cell that I am trying to access the value:
<TD><INPUT TYPE="text" NAME="in_newfunding" SIZE="14" MAXLENGTH="14" VALUE="2877488.74" onKeyUp="elements[3].value = makemoney(elements[2].value - elements[1].value)"></TD>
When I use this alert:
alert(tblrows[2].childNodes[4].innerHTML);
the output is:
<input onKeyUp= ... size=14 maxlength=14 name=newfunding value=2877488.74>
To access the value in that cell, I tried the following:
alert(tblrows[2].childNodes[4].value);
I get a "unknown" message from the alert.
Please help me as I don't understand why I am getting this message.
Thank you.
rogers
09-23-2003, 10:58 AM
I guess Adam is not on the forum today.
Can anyone else help? This is a bit urgent.
Thank you.
AdamBrill
09-23-2003, 04:20 PM
Ok... I would say that most of your problems are coming from the fact that the tables in the page are completely screwed up. ;) But, I would suggest that you give each of the cells that you need to access an ID, rather than giving it to the table and then going through the nodes. It would be much easier...
Or, if you are only trying to access the form elements, don't use getElementById at all, but access them like this:
document.formname.elementname.value
Since I'm not sure exactly what you are trying to do(ie, access the actual rows of the table or the form elements that are in them), it is hard for me to give you the code. I hope this will help you and feel free to post any other questions that you might have. If you can't figure it out, please try to describe exactly what you are trying to do and I will do my best to help...
BTW, I am still in school, so that is why I wasn't around all morning... ;)
rogers
09-24-2003, 08:27 PM
Hi Adam,
I had a text field inside a <td>, and so I had to refer to it as childNodes[i].childNodes[0].value.
I was able to solve the problem. Thanks for your help.