0

I'm trying to get to work a page that says the date with a "index.html" including a javascript file. However, I don't know if I'm missing something here but when testing it, it won't appear in the index.html. I just want the date to be shown with the code of the js. Here's the code of the index.html:

<!DOCTYPE html>
<head>
    <title>Test</title>
    <meta charset="utf-8">
    <script type="text/javascript" src="scripts/date.js"></script>
</head>
    <script>
    <p id="test1"></p>
    </script>

And here's the code of the javascript file:

var isDate = new Date();

document.getElementById("test1").innerHTML = isDate.getDate;

As you can see, I tried puting script tag around the p tag and dindn't work either.

This is really basic javascript but it's like really hard for me to start off in a new language. Thanks.

Txni.26
  • 43
  • 1
  • 4

5 Answers5

1

use the defer attribute like:

<script type="text/javascript" src="scripts/date.js" defer></script>

then it should work just fine.

The reason for this is that putting the script tag in the head will run the script before the content of the body is parsed so using defer will make the script run after all the questions have been parsed.

  • Thanks to the last thing you said combined with other reply I could solve the problem! Thank you very much ^^. – Txni.26 May 07 '21 at 14:25
1

try correcting your HTML:

<!DOCTYPE html>
<html>
<head>
    <title>Test</title>
    <script type="text/javascript" src="scripts/date.js"></script>
</head>
<body>

<p id="test1"></p>

</body>
</html>
AdTCode
  • 25
  • 7
1

This is the code your looking for:

<div>
    <p id="test1"></p>
</div>

<script>
    var isDate = new Date();

    document.getElementById("test1").innerHTML = isDate.getDate();
</script>
Prosy Arceno
  • 2,616
  • 1
  • 8
  • 32
0

so console.log will be your new best friend, Throw that into the file and see if your console shows any of the ouput. my guess is that you Also you did not put the p tag into a body, also dont wrap html elements in script tags.

<!DOCTYPE html>
<head>
    <title>Test</title>
    <meta charset="utf-8">
    <script type="text/javascript" src="scripts/date.js"></script>
</head>
<body>
    <p id="test1"></p>
</body>
</html>

and scripts/date.js

window.onload = function() {
    var currentDate = new Date();
    var paragraph = document.getElementById("test1");
    if (paragraph) {
        paragraph.innerHTML = currentDate.getDate();
    }
}
Michael Mano
  • 3,339
  • 2
  • 14
  • 35
0

The format for the HTML document is incorrect. Surrounding <p> with <script> doesn't make sense because <script> is for Javascript code whereas you wanted to create HTML elements. You want <p> to be the child of <body> to display the element.

As for using the Date function in JavaScript, here is a related question to get the format correct.

<!DOCTYPE html>
<html>
    <head>
        <title>Test</title>
        <meta charset="utf-8">
        <script type="text/javascript" src="scripts/date.js"></script>
    </head>

    <body>
        <p id="test1"></p>
    </body>

    <script>
        var isDate = new Date();
        document.getElementById("test1").innerHTML = isDate.getDate();
    </script>
</html>
Tangy
  • 85
  • 9
  • 1
    Thanks to your reply and combining other I could solve the problem. The problem was that I was keeping the script tag that links the html to the folder with the scripts was in between head tags. I put it in the bottom of the code and it was flawlessly! Thanks for helping :) – Txni.26 May 07 '21 at 14:24