0

I am trying to use year multiple times in an HTML page using the below script

$('#year').text(new Date().getFullYear());

It is getting displayed first time but 2nd time it is not working. I am using below code in html

<p>Copyright &copy;
  First Time<span id="year"></span> - Second Time<span id="year"></span>
</p>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8"> 
  <title>Jquery Date issue</title>
</head>
<body> 
    <p>Copyright &copy;
            First Time<span id="year"></span> - Second Time<span id="year"></span>
    </p>
  <script src="http://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
    crossorigin="anonymous"></script>

  <script>
    // Get the current year for the copyright
    $('#year').text(new Date().getFullYear());   
  </script>
</body>

</html>

The code is here https://jsfiddle.net/vptechworld/z0gmobyw/5/

1 Answers1

0

You can not have two elements with same ID, but you can use same class.

<p>Copyright &copy;
  First Time<span class="year"></span> - Second Time<span class="year"></span>
</p>
$('.year').text(new Date().getFullYear());
tobias
  • 934
  • 8
  • 17
vmf91
  • 1,897
  • 19
  • 27