-3

I'd like to simply display the current year on a span, I tried yesterday on my dev side site, it wokrs directly. When I tried to do it again (because it wasnt saved ..) I just can't do it anymore and I can't figure what I'm doing differently from yesterday. Please help

const currentYear = document.getElementById('currentYear')

function showYear() {
    currentYear.write(new Date().getFullYear());
}
showYear();
<p>© <span id=currentYear></span> DevWeb</p>
David
  • 208,112
  • 36
  • 198
  • 279
Kikay
  • 1
  • 1
  • 2
    Elements don't have a `write` method. – Quentin Nov 29 '22 at 16:11
  • 2
    There's an error on the browser's development console telling you what the problem is. Always check for errors first. – David Nov 29 '22 at 16:13
  • I swear I'm seeing myslef typing the .getElementById for remplacing the "document".write.. And it works, did I smoke too much or I did something else that I can't remember ? – Kikay Nov 29 '22 at 16:18

1 Answers1

1

Just use:

currentYear.textContent = new Date().getFullYear();

example in jsFiddle : https://jsfiddle.net/p1et386y/

Julian
  • 103
  • 1
  • 7
  • Thank you! I didn't knew this one. It works, but I'm still confused about my method, I swear I'm seeing myslef remplacing the "document" from document.write by a const called with getElementById.. Maybe I did something diffently but I really can't remember – Kikay Nov 29 '22 at 16:21