+ 8

Code works on my computer but won't run on the SoloLearn IDE...

(Edited after @naomi 's answer) This is the code: var e = document.getElementById('container') var v = gridSize var sizePercentage = 100/gridSize for(i = 0; i < v; i++){ var row = document.createElement("div") row.className = "row" row.style.height = `${sizePercentage}%` row.style.width = '100%' for(x = 0; x < v; x++){ var cell = document.createElement("div") cell.className = "gridsquare" cell.id = `id${i}${x}` // cell.innerText = '1' // cell.innerText = `${i}${x}` cell.style.height = '100%' cell.style.width = `${sizePercentage}%` row.appendChild(cell); } e.appendChild(row); } I get this message: Uncaught TypeError: Cannot read property 'appendChild' of null Line: 30 Line 30 is the second to last : e.appendChild(row); Thanks!

11th Jul 2018, 12:27 AM
Alejandra Camus
Alejandra Camus - avatar
15 Respuestas
+ 7
The JS in SoloLearn IDE is included in the head of the document I suppose. This may results in some elements of the body will not be completely created when the script run. Try putting your code in window.onload, this ensures your script will run only when all elements are loaded/created: window.onload = function() {// your code goes here}
11th Jul 2018, 2:34 AM
Hoàng Nguyễn Văn
Hoàng Nguyễn Văn - avatar
+ 4
Nguyễn Văn Hoàng actually the script was at the end of the body tag, but your suggestion still worked! Thanks a bunch! I share the working code: https://code.sololearn.com/WS9J7xJ1ZX8L/?ref=app
11th Jul 2018, 1:28 PM
Alejandra Camus
Alejandra Camus - avatar
+ 2
e is not defined, at least the code you provided. The error (line 30) is the first occurrence of e. What is it defined as/how do you define it?
11th Jul 2018, 2:38 AM
naomi
naomi - avatar
+ 2
naomi I did define e, just made a mistake and didn't copy it in the code! I edited it now. Nguyễn Văn Hoàng's answer worked though! https://code.sololearn.com/WS9J7xJ1ZX8L/?ref=app
11th Jul 2018, 1:26 PM
Alejandra Camus
Alejandra Camus - avatar
+ 1
Kunj write Javascript, HTML using any text editor save it with extension html / js and open in your web browser
21st Jul 2018, 12:46 AM
code learner
code learner - avatar
+ 1
In sololearn, HTML is read from top to bottom, so sololearn starts with js code first then your id=container in this case, id appendChild will be null because js executed before container was defined. a fix to this issue is placing the JS code below the ID container inside the HTML instead of sololearn JS code like: <HTML> <head> </head> <body> <div id="container"> </div> <script> <!-- put your JS code here --> </script> </body>
23rd Jul 2018, 11:16 PM
Core i9
Core i9 - avatar
0
Let me try this......
19th Jul 2018, 10:22 AM
Saksham Kumar
0
If I want to run JavaScript code on laptop which software should I use @Alejandra Camus
20th Jul 2018, 12:21 PM
Kunj Shah
Kunj Shah - avatar
0
Alejandra Camus, You're beautiful, you know the code, you live in Mexico! I'll teach you how to cook borsch! Will you marry me?! PS.im Alexander, from Russia (Saint Petersburg), with love 😂😄😘 Stellar Fox: Keep it related to SL and coding :P
30th Jul 2018, 11:53 AM
Nu11
Nu11 - avatar
0
Haha😂😂..y some guys are crazy about you Alejandra Camus
18th Aug 2018, 8:55 AM
Oblesh Valmiki
Oblesh Valmiki - avatar
0
Stellar Fox I don't have any negative impacy. I liked her code thats why I want to make an contact.I am week to girls ok.And please don't reflect s your cheap personality by commented as such. Stellar Fox: Indranil I just informed you about the rules of the forum. You're the one who is jumping to conclusions.
19th Aug 2018, 3:25 PM
Indranil Dutta
Indranil Dutta - avatar
0
ok let be forumed.
19th Aug 2018, 3:50 PM
Indranil Dutta
Indranil Dutta - avatar
- 3
you are beautifull
19th Jul 2018, 4:23 PM
AppDeveloper
AppDeveloper - avatar
- 3
i am from isb.....
19th Jul 2018, 4:23 PM
AppDeveloper
AppDeveloper - avatar
- 3
Great... Alejandra Camus can I know your Facebook profile name?? Stellar Fox: Indranil Dutta Sharing personal information is prohibited on Sololearn.
21st Jul 2018, 11:59 AM
Indranil Dutta
Indranil Dutta - avatar