Ask coding questions

← Back to all posts
How to set innerHTML to a variable?
RixTheTyrunt

Hi everyone, I have a problem, again. It shows me this:

Can somebody fix this? Did I made something wrong?

Answered by Coder100 [earned 5 cycles]
View Answer
Voters
RixTheTyrunt
Comments
hotnewtop
Coder100

The error isn't the variable the error is the element.

You put the script.js before any of the HTML was set, so there are 2 ways to fix this.

move script tag

move it to the end of the page like this:

add window onload

wait until the page has loaded before executing

RixTheTyrunt

No, you can place it not just in the end of the <body>, you can place it in the <head> too

RixTheTyrunt

What does window.onload do? ( Just curious )

Coder100

when the window loads @SnackyPlusPlus

Coder100

yeah but then you have to use option 2 @SnackyPlusPlus

JustCoding123

You are getting this error because you are using the same script file in index.html too and index.html hasn't got an element with the id currentTime. Edit: Sorry I didn't see the other currentTime in index.html

RixTheTyrunt

@JustCoding123 I've made a invisible paragraph with a id currentTime

MrVoo

You should put dateNow on the opposite side of the equals sign. The way you wrote it now makes it look like you're redefining innerHTML

RixTheTyrunt

@MrVoo What do you mean?

RixTheTyrunt

@MrVoo Show me a code