Calculate age onchange of dob textbox using jQuery

<input type="date" style="width:210px" name="txtdateofbirth" onChange="calage(this.value)" id="txtdateofbirth">
function calage(dob) {
var date1 = new Date(); 
var y = date1.getFullYear(); 
var by = dob.split("-",1); 
var age = y - by; $("#txtage").html(age); 
}

Comments

  1. I tried it on jsfiddle but it's not work. Could you give me a correction please?

    http://jsfiddle.net/nobuts/3vRvV/

    ReplyDelete
    Replies
    1. @bonus: Hi bonus. It is nice to see that you are using this code.
      And also thank you for asking this question. I am sorry, but I made a mistake. While assigning calculated age to span, we should write $("#txtage").html(age);

      And in jsfiddle, you have choose proper js version need to be included and when js code should be added in the page.

      Please go through this and observe "Frameworks & Extensions" section at the left:

      http://jsfiddle.net/3vRvV/8/

      Delete

Post a Comment

Please feel free to comment. I would love to hear feedback.