I want to get birthyear in textbox
and show the age in alert
but it shows NaN
.
function getAge(birth){
//get current date
var now = new Date();
var currentYear = now.getFullYear();
//get bithday
var birthYear = birth;
//calc
var age = currentYear - birthYear;
// var age_month = currentMonth - birthmonth;
return age;
}
function getIn(){
var getTextBox = document.getElementById("txtInput");
var getVal = getTextBox.value;
var birth = new Number(getVal);
alert (getAge());
}
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="javascript.js"></script>
</head>
<body>
<form>
<input type="text" id="txtInput"/>
<input type="button" value="Calc" onclick="getIn()"/>
</form>
</body>
</html>
alert (getAge());
should you be adding the age in the call:alert (getAge(birth));
?birthYear
variable ingetAge
function. You can just use the one passed from the function call.