Javascript createTextNode doesn't work

137 views Asked by At

I have an assignment at my school to write the DOM of the webpage when JavaScript has executed. We got the assignment on paper and when I tried to write it on sublime and run it the code won't work. I think the problem is at the following lines:

tdTextNodeRef=document.createTextNode(codeNodeRefs[counter].innerHTML);
        tdNodeRef.appendChild(tdTextNodeRef); 

The full code can be found here: https://github.com/Nirakander/JS-assignment

Why won't this work? Really bad assignment when the code is not even working. Thanks in advance!

1

There are 1 answers

0
RobG On

Your error is here:

for(var counter=0; counter<codeNodeRefs.length; counter++);{
----------------------------------------------------------^

That extra semicolon ends the if statement with an empty block, the following block is seen as just a block of statements. So it loops around twice, does nothing, then goes into the block.

When entering that block, count is codeNodeRefs.length (i.e. 2), so codeNodeRefs[counter] returns undefined.

Just remove the semicolon.