Sagspot
Sagspot

Reputation: 45

Display current year using external javascript

I'm having trouble updating the current year using js from an external file. The year in the html doesn't update to display the current year. Here's my html code;

<p>
    Copyright &copy; <span id="year">year</span>. All rights reseverved.
</p>

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

And here's my js code;

const year = document.querySelector('#year');

function date() {
    year.innerHTML = new Date()
};

What am I doing wrong here, and what's the right way to achieve my goal?

Upvotes: 1

Views: 1579

Answers (1)

Pavlo
Pavlo

Reputation: 649

Just try to add function invocation:

  document.addEventListener('DOMContentLoaded', function(){
      const year = document.querySelector('#year');

      function date() {
         year.innerHTML = new Date().getFullYear();
      };

      date();
  }

Upvotes: 3

Related Questions