For people who have the same issue of querySelector or getElementById that returns the following error:
Uncaught TypeError: Cannot read property ‘appendChild’ of null
but you have a class name or id in the HTML…
If your script tag is in the head, the JavaScript is loaded before your HTML. You will need to add defer to your script like so:
There isn’t an element on your page with the id “mainContent” when your callback is being executed.
In the line:
document.getElementById(“mainContent”).appendChild(p);
the section document.getElementById(“mainContent”) is returning null