0

how can i fix that

i have a program in that program. <script type="text/javascript"> let my=document.getElementById('mylist'); let li=document.createElement('li'); li.className='my'; li.id='na'; var text=document.createTextNode('never'); li.appendChild(text); var x=document.createElement("p"); var t=document.createTextNode('this is a paragraph'); x.appendChild(t); document.body.appendChild(x); </script> <title></title> </head> <body> <ul id="mylist"> <li>my</li> </ul> in which appendChild did not work replace.html:14 Uncaught TypeError: Cannot read property 'appendChild' of null they give me that error plzz how can i fix that

4th Jun 2020, 6:10 AM
Mohammad Waqas
Mohammad Waqas - avatar
2 odpowiedzi
+ 3
You're running the script in the head. The body portion of the DOM hasn't been created yet. Try moving the script to the end of the body, or wrap the code in an onload function so that it runs after the DOM has been created.
4th Jun 2020, 6:38 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Thanks
4th Jun 2020, 7:08 AM
Mohammad Waqas
Mohammad Waqas - avatar