2

I've calculated the age using setFullYear and getFullYear, as I only want to use the birth year to calculate age for fun, but it doesn't work

Here's the code I've tried so far, which I think should work. Please give me some pointers:

function Onclick() 
{
     var Text1 = $("#Text1").val();

     var datetoday = new Date();
     var today = datetoday.getFullYear();
     var datebirthday = new Date();
     datebirthday.setFullYear(Text1);
     var birthday = datebirthday.getFullYear();
     var age = today - birthday;

     $("#txtAge").text(age);
 }
2

It was a simple mistake as I assumed, as it is a textbox and not a div, I should use .val instead of .text or html or innerHTML, like this:

function Onclick() {
var Text1 = $("#Text1").val();
var datetoday = new Date();
var today = datetoday.getFullYear();
var datebirthday = new Date();
datebirthday.setFullYear(Text1);
var birthday = datebirthday.getFullYear();
var age = today - birthday;
$("#txtAge").val(age);

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.