I have to sets of dates (via inputs/selects - mm/dd/yyy) on page. I want certain readonly text field to show age in months (calculated based on those two dates) after a certain select field (different select, not a part of dates set) has been changed (any option in that select chosen must trigger calculation and show result in readonly field). It should be pretty simple, but doesn't work, would appreciate any advise. This is what I have so far:
$(document).ready( function (){ // bind the recalc function to the select field $("#certain_select").bind("change", recalc); // run calculation recalc(); } ); function recalc(){ var b_day=parseInt($("#dob_dd").value); var b_mo=parseInt($("#dob_mm").value); var b_year=parseInt($("#dob_yy").value); var visit_day=parseInt($("#visit_dd").value); var visit_mo=parseInt($("#visit_mm").value); var visit_year=parseInt($("#visit_yy").value); var age; if (visit_year==b_year){ age=(visit_mo-b_mo); $("#showage").text(age.toString()); }else if(b_year<visit_year){ var age_years=(visit_year-b_year); var age_months=(visit_mo-b_mo); age=(12*age_years + age_months); $("#showage").text(age.toString()); }else{ age=0; $("#showage").text(age.toString()); } }//end of recalc function