0

TypeError: Cannot set property 'innerHTML' of null

Here is my code: It works fine if I code a string directly into the thisLocation value, eg "A2". But if I allow thisLocation to get its value from the array I get a TypeError: Cannot set property 'innerHTML' of null. However even though I get the error the alert box still dsiplays the thislocation value correctly. var arr = letters.split("|"); var thisImage; var thisLocation; for(i =0; i < arr.length; i++){ thisLocation = arr[i]; alert("thisLocation is " + thisLocation ); document.getElementById(thisLocation).innerHTML = "<img src=" + thisImage + ">"; }

3rd Apr 2020, 4:25 AM
Bill
2 Réponses
+ 1
Copy your entire code to CodePlayground and link it here. In your arr you have strings that don't match any DOM object id. Temporary solution: var el = document....(thisLocation); if(el){ el.innerHTML = "....."; } Link your code.
3rd Apr 2020, 4:40 AM
Kevin ★
0
Thanks very much Kevin. That solved it for me.
4th Apr 2020, 3:27 AM
Bill