I’m not able to change the text at “Enter your age” to “your age is” when submitting. Is there way that when I submit the DOB the “Enter you age” changes to “your age is: X”?
function myFunction() {
var dobString = document.getElementById("birth_date").value;
var calculatedValue = calculate_age(dobString);
}
function calculate_age(dobString) {
var diff_ms = Date.now() - Date.parse(dobString);
var yearString = "";
var yearValue = Math.floor(diff_ms / 31536000000);
console.log("Years ", yearValue);
if (yearValue > 1)
yearString = " years";
else
yearString = " year";
if (yearValue > 0)
var calculatedValue = yearValue + yearString + " ";
else if (yearValue == 0)
var calculatedValue = monthValue + monthString + " ";
document.getElementById("age").innerHTML = "Your Age is : " + calculatedValue;
}
<form>
Birthday: <input type="date" name="bday" id="birth_date">
<br><br>
</form>
<button onclick="myFunction()">view</button><br>
<h2>Enter your age</h2>
<p id="age"></p>
3
Answers
Update template to be
And update script to be like this
You need to calculate your age a bit differently.
Maybe something like this:
I created a fiddle for you to see it in action:
https://jsfiddle.net/cdrsfo82/
It’s simple, just put
id="age"
on the<h2>