How to make my JavaScript excute only after the html document is completely loaded? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

How to make my JavaScript excute only after the html document is completely loaded?

I wrote a code in the sololearn web playground but my javascript show a 'cant execute appendChild property of null' and i'm sure it's becsuse the js file is liaded before the element i want to change.

12th Dec 2020, 12:19 PM
Elon
Elon - avatar
4 Answers
+ 5
Elon Yemp You can write JavaScript code inside window.onload function or inside this $(document).ready(function () { //js.code } window.onload = function () { //js code }; document.addEventListener("DOMContentLoaded", function (event) { //js code }); window.addEventListener('load', function () { js code }); You can use any one from the above function.
12th Dec 2020, 12:23 PM
A͢J
A͢J - avatar
+ 3
It worked ! Thanks !
12th Dec 2020, 12:41 PM
Elon
Elon - avatar
+ 2
Ok im gonna try it
12th Dec 2020, 12:26 PM
Elon
Elon - avatar
+ 2
AJ #Learn With Me 👨‍💻 I think you have to mention that the 1st example assumes jquery
12th Dec 2020, 12:43 PM
Nyakotey🇬🇭🎹
Nyakotey🇬🇭🎹 - avatar