Trouble displaying javascript in HTML

Hello guys, I’m not sure why my code isn’t working and it doesn’t print the date in the browser. Could you please help me?

<!DOCTYPE html>
<html>
  <head>
    <title>Parcel Sandbox</title>
    <meta charset="UTF-8" />
    <script src="src/index.js"></script>

    <script type="text/javascript">
      function myFunction() {
        var today = new Date();
        var date = today.getDay();
        var daylist = [
          "Sunday",
          "Monday",
          "Tuesday",
          "Wednesday ",
          "Thursday",
          "Friday",
          "Saturday"
        ];

        var dayOfTheWeek = daylist[date];

        document.getElementById("myText").innerHTML = dayOfTheWeek;
      }
    </script>
  </head>

  <body onload="myFunction()">
    <div id="app"></div>

    <h1>
      Today is
      <script type="text/javascript">
        document.write(dayOfTheWeek)
      </script>
    </h1>
  </body>
</html>

there is no element with such an id

<body onload="myFunction()"> is also very unusual and same goes for

<script type="text/javascript">
        document.write(dayOfTheWeek)
      </script>

I’d recommend you start with a basic JS tutorial.

https://javascript.info can also help you on how to interact with the DOM (writing from JS to HTML)

1 Like