Click to See Complete Forum and Search --> : getElementById doesn't work


dirku
03-05-2004, 08:19 AM
Hi.

I using JavaScript to dynamically create a table with a number of <TD> depending on the user who logged on (some want to see three values and others want to see five values...):


<SCRIPT language="JavaScript1.2">
document.write('<table class="clocks" border="1" bordercolor="#11fff1" width="100%" cellspacing="0" cellpadding="0" height="35">');
document.write('<tr>');
for(x=0; x<numOfTimezones; x++)
{
document.write('<td ID="Clock"' + x + ' valign="top" align="center" style="font-family: Verdana, Tahoma, Arial; font-size: 10px; padding-top: 2px;" >Clock' + x + '</td>');
}//end for x
document.write('<(tr>');
document.write('</table>');
</SCRIPT>


To fill the variable number of <TD>s I am using some JavaScript functions. Of course, this should happen in a for-loop. In this loop I want to use a code like this to get the element and then put a value into the <TD>:


for(x=0; x<numOfTimezones; x++)
{
document.getElementById('Clock'+x).innerHTML =
'<font color="' + ct[x].cl + '">' + ct[x].tz + ClockString(ct[x].ct) + '</font>' ;
}//end for x


What can I do to access thes elements using JavaScript?

Thank you,

Dirk

jaegernaut
03-05-2004, 08:37 AM
I see two things that may be causing a problem:

1. When you are writing the ID in the TD the variable is concatenated after the double quotes of Clock, so the ID for every TD created will still be Clock.
Example: Original: document.write('<td ID="Clock"' + x + ' valign="top".....
Change to document.write('<td ID="Clock' + x + '" valign="top"......
This should give you Ids of Clock0, Clock1, etc.

2. You have a lone parenthisis in this line: document.write('<(tr>'); So the TR isn't closed.

dirku
03-05-2004, 09:10 AM
Thank you, jaegernaut.

No it works very well.

Regards,

Dirk
Berlin, Germany

jaegernaut
03-05-2004, 09:16 AM
You're welcome. I'm glad to help.