-1

I want to calculate the age automatically after a date is selected, but it's not working in jquery-3.3.1, I hope you guys can help me

<script type="text/javascript">
$('#tgl_lahir').datepicker({
    onSelect: function(value, ui) {
        var today = new Date(), 
            age = today.getFullYear() - ui.selectedYear;
        $('#umur').val(age);
    },
    maxDate: '+0d',
    changeMonth: true,
    changeYear: true,
    defaultDate: '-18yr',
});
</script>
Bani
  • 542
  • 4
  • 20
DMCorner
  • 1
  • 1
  • 3
    `but its not working` is not a helpful description of your problem. Do you get an error message? Do you get the wrong age? Please explain what exactly is not working. – t.niese Jul 11 '22 at 07:38
  • `Let alone it is working` – IT goldman Jul 11 '22 at 09:52
  • i mean supposedly age calculation result appear in automatically after selecting date in datepicker, and it doesn't happen – DMCorner Jul 11 '22 at 09:57

1 Answers1

0

For me it works.

$('#tgl_lahir').datepicker({
  onSelect: function(value, ui) {
    var today = new Date(),
      age = today.getFullYear() - ui.selectedYear;
    $('#umur').val(age);
  },
  maxDate: '+0d',
  changeMonth: true,
  changeYear: true,
  defaultDate: '-18yr',
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.13.1/jquery-ui.min.js"></script>
<link href="https://code.jquery.com/ui/1.13.1/themes/base/jquery-ui.css" rel="stylesheet">

birth: <input id="tgl_lahir"><br>
age: <input id="umur">
IT goldman
  • 14,885
  • 2
  • 14
  • 28
  • thanks for your help..but i want to use the input element so that it can be inserted into the database.. – DMCorner Jul 11 '22 at 10:42