Im trying to output my birthday within my site using javaScript, but I seem to be getting the following error
Uncaught TypeError: Cannot set property 'innerHTML' of null
Heres my HTML - pretty basic
<body>
<span id="currentAge"></span>
<script type="text/javascript" src="/assets/js/function.js" ></script>
</body>
Heres my JavaScript code
window.onload = function() {
function getAge(dateString) {
var today = new Date();
var birthDate = new Date(dateString);
var age = today.getFullYear() - birthDate.getFullYear();
var month = today.getMonth() - birthDate.getMonth();
if (month < 0 || (month === 0 && today.getDate() < birthDate.getDate())){
age--;
}
var myAge = document.getElementById('currentAge').innerHTML = age;
}
//call function
getAge("1991/03/09");
}
edit: my js script is at the bottom of my body element
Aucun commentaire:
Enregistrer un commentaire