Skip to content Skip to sidebar Skip to footer

What Is Wrong With My Below Code For Javascript?

I want to display the day and date in following format but it doesn't work for me. Error showing 'Uncaught TypeError: Cannot set property 'innerHTML' of null'. Can anyone get me ou

Solution 1:

Try calling the script at the bottom of the html.

<!DOCTYPE html><html><head><metacharset=utf-8 /><title>JS Excercise</title></head><body>
  Today is : <spanid="day"></span><br/><br/>
  Current time is : <spanid="time"></span></body><scriptsrc="Js\DateTimeExcercise.js"></script></html>

Solution 2:

You load the script before the DOM is rendered. So it's better to place the script tag below all your code, just above the body tag. Right now the code cant find the elements yet

Another possibility is to wrap your code in a event listener, like this:

document.addEventListener("DOMContentLoaded", function()
  // your code
});

Post a Comment for "What Is Wrong With My Below Code For Javascript?"