0

Is it possible to print the current year inside an input value with javascript? PS.: I don't want to use the type="date" method. PS. 2: The value needs to be printed on the input as soon as the screen is opened.

var dateControl = document.querySelector('input.year[type="text"]');
dateControl.value = "getFullYear()";
<input type="text" id="year" value="">  

10
  • 1
    The linked duplicate shows you how to get the year. Aside from that, your selector should simply be: document.querySelector('#year') since you can select on an id. Commented Dec 27, 2021 at 18:49
  • Unfortunately this solution doesn't work for me, as I've already tried it. Commented Dec 27, 2021 at 18:50
  • Can you update the code shown to demonstrate that attempt and indicate what specifically isn't working? Commented Dec 27, 2021 at 18:50
  • 1
    try ` dateControl.value = new Date().getFullYear(); ` Commented Dec 27, 2021 at 18:53
  • 1
    @Mari: In short, when applying the information present in the linked duplicate, what happens when you just do this?: document.getElementById('year').value = new Date().getFullYear(); Commented Dec 27, 2021 at 18:55

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.