Search code examples
javascripthtmlinnerhtmlgetelementbyid

Issue with .getElementByID (linking html & js)


I'm a month into coding, so still pretty new. I'm currently having an issue linking my js file to my html file. Because of this issue, my javascript isn't showing in the browser. I'm not sure what the issue is (is it with document.getElementById('clock')? Should I add innerHTML)? if anyone could help me figure out how to link these files, that would be great. Thanks so much!

<html>
    <head>
        <link href="clock.css" type="text/css" rel="stylesheet"></link>
        <title>TIME Time!</title>
        <link rel="preconnect" href="https://fonts.gstatic.com">
        <link href="https://fonts.googleapis.com/css2?family=VT323&display=swap" rel="stylesheet">
    </head>

    <body class="mainbod"> 

        <p class="tagline">Time for the time!</p>
        <div class="zaddy">
            <div class="clock">clock goes here</div>

        <script src="clock.js"></script>

    </body>

</html>`

      
var morning = 6;
var noon = 12;
var evening = 17;
var night = 20;

var showCurrentTime = function() {

    var clock = document.getElementById('clock');
    var currentTime = newDate();

    var hours = currentTime.getHours();
    var minutes = currentTime.getMinutes();
    var seconds = currentTime.getSeconds();
    var meridian = "AM";

        if (hours >= noon) {
            meridian = "PM";
        }
        if (hours > noon) {
            hours = noon - 12;
        }
        if (minutes > 10) {
            minutes = "0" + minutes;
        }
        if (seconds < 10) {
            seconds = "0" + seconds;
        }
var clockTime = hours + " : " + minutes + " : " +  seconds + "  " + meridian;

clock.innerText = clockTime;
}

Solution

  • First you are trying to use document.getElementById but you don't have any id set , so your html code should look like

    <div id="clock">clock goes here</div>

    Second, in your javascript methods, the method is defined but never called, also there is a space required between new and Date(); so your js file should look like

    var morning = 6; 
    var noon = 12; 
    var evening = 17; 
    var night = 20;
    
    showCurrentTime = function() { 
      var clock = document.getElementById('clock'); 
      var currentTime = new Date(); 
      var hours = currentTime.getHours(); 
      var minutes = currentTime.getMinutes(); 
      var seconds = currentTime.getSeconds(); 
      var meridian = "AM";
    
        if (hours >= noon) {
            meridian = "PM";
        }
        if (hours > noon) {
            hours = noon - 12;
        }
        if (minutes > 10) {
            minutes = "0" + minutes;
        }
        if (seconds < 10) {
            seconds = "0" + seconds;
        }
      var clockTime = hours + " : " + minutes + " : " + seconds + " " + meridian; 
      clock.innerText = clockTime; 
    }
    
    showCurrentTime();
    
    

    if you can't load your js file, make sure that your html and js file are both in same folder or directory