Hi, there.
Why does the code below returns a message on the console “pNode is null”, and not a reference to the element with id=“pNodeId”?
What could be going wrong?
function pClick(event) {
alert("Click on p.");
}
var pNode = document.getElementById("pNodeId");
pNode.addEventListener("click", pClick);
Hint.: the id of the element (pNodeId) is strictly orrect.
I see the problem, the <script> section needs to be after the HTML with the ID you’re targeting. The script is running before its been loaded, so it can’t find the ID it’s looking for.