-2

my js script :

var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0'); 
var yyyy = today.getFullYear();

today = mm + '/' + dd + '/' + yyyy;


document.getElementById("heure").innerHTML = today; 

my html code :

<!DOCTYPE html>
<html lang="fr">
<head>
   <script src="../js/index.js" type="text/javascript"></script>
</head>
<body>
   <p>
       Il est :    <span id="heure"></span>
   </p>
   <p>
       et vous etes actuellement sur mon site, quel honneur.
   </p>
</body>
</html>

the alert to test the javascript file work, document.write(today) work, realy i dont understand...

2 Answers2

0

You are trying to get the element by ID before it is initiated. Call the script which has getelementbyID after the span or the body tags.

Priyanjana
  • 11
  • 2
0

Try using script tag at the end of body, most probably the issue here is that, the script is loading before DOM, because of which it cannot recognise span tag

Ishan
  • 77
  • 3