Why is the input type "date" value not updating new values from the input

2k views Asked by At

I am trying to get a date from the input type "date" but when I retrieve the data in JavaScript, only the default value that I set is retrieved all the time.

No matter what I do, I keep getting the default value instead of the new one I chose.

I couldn't find any solution from all the similar questions about this, please do not flag this question because I am already getting a warning that my last question was not well received by the community, I thought we are here to learn and asking stupid questions is part of it.

var getInput = document.querySelector("#year");
var data = getInput.value, dob = data.split("-"), byy = dob[0], bmm = dob[1], bdd = dob[2];

function process(){
console.log(byy)
}
<input id="year" type="date" value="2022-12-25" required><br>
<input id="goBtn" onclick="process()" type="button" value="Go">

1

There are 1 answers

0
Stack Underflow On BEST ANSWER

Get the date value within the function you are calling.

function process() {
  var getInput = document.querySelector("#year");
  var data = getInput.value, dob = data.split("-"), byy = dob[0], bmm = dob[1], bdd = dob[2];
  console.log(byy);
}
<input id="year" type="date" value="2022-12-25" required><br>
<input id="goBtn" onclick="process()" type="button" value="Go">

The variables were being assigned to immediately when the JavaScript executed, giving them the default value. They need to be assigned to at the time you press the go button.