JavaScript TypeError

For some reason, I get “Uncaught TypeError: Cannot set property ‘innerText’ of null at” when gr1 and other variables are supposed to be changed into an item from an array

    let rasa = document.getElementById("prvi")

let rase = ["jedna", "druga", "treca", "cetvrta"]

let gr1 = document.getElementById("#gr1")
let gr2 = document.getElementById("#gr2")
let dl1 = document.getElementById("#dl1")
let dl2 = document.getElementById("#dl2")
let niz = ["jd","dv","tr","ct","pt","s"]
function dd(){

rasa.innerText = rase[Math.floor(Math.random()*4)]
gr1.innerText = niz[Math.floor(Math.random()*6)]
gr2.innerText = niz[Math.floor(Math.random()*6)]
dl1.innerText = niz[Math.floor(Math.random()*6)]
dl2.innerText = niz[Math.floor(Math.random()*6)]


}

see usage examples here:

you may notice the issues, it’s pretty trivial - if you notice it, that’s it

1 Like

Thanks, this happens quite often to me :sweat_smile: :man_facepalming:

Now the other problem appeared, the gr1 variable is working but I still get the same notification from the console for the other three

what have you changed? what’s your new code?

Same as the one I posted up but I have removed “#” from the getElementById() so it looks like this now

    let rasa = document.getElementById("prvi")

let rase = ["jedna", "druga", "treca", "cetvrta"]

let gr1 = document.getElementById("gr1")
let gr2 = document.getElementById("gr2")
let dl1 = document.getElementById("dl1")
let dl2 = document.getElementById("dl2")
let niz = ["jd","dv","tr","ct","pt","s"]
function dd(){

rasa.innerText = rase[Math.floor(Math.random()*4)]
gr1.innerText = niz[Math.floor(Math.random()*6)]
gr2.innerText = niz[Math.floor(Math.random()*6)]
dl1.innerText = niz[Math.floor(Math.random()*6)]
dl2.innerText = niz[Math.floor(Math.random()*6)]


}

and what’s the rest of your code?


Here it is

what are you using for debugging?

it doesn’t work but in codepen there is none of the errors you say

Nothing, I google the problem and when I can’t find the solution there, I post it here. I don’t use anything else

where do you see the errors?

it doesn’t work but in codepen there is none of the errors you say

ok, you do not have a #dl1 element

1 Like


Here, all four blocks are supposed to have the text from the array in themselves

Thank you so much! It would take me hours to figure this out

use the Analyze HTML feature in codepen next time

1 Like