allogos allogos - 6 months ago 18
jQuery Question

jquery change event on date calculation

I add X number of months by Dropdown (month_number) to my start date so end date will be calculated and shown correctly:

$('#start_date').change(function(){
var months = +$('#month_number').val();
var end = new Date($(this).val());
end.setMonth(end.getMonth() + months);
$('#end_date').val( (end.getMonth() + 1) + '/' + end.getDate() + '/' + end.getFullYear()) ;


Demo: https://jsfiddle.net/8epztLv2/3/

But i want by changing the dropdown (month_number) end date is not changing !

Answer

Here's a fiddle.

$('#start_date, #month_number').change(function() {
  var months = $('#month_number').val();
  var end = new Date($('#start_date').val());
  end.setMonth(end.getMonth() + Number(months));
  $('#end_date').val((end.getMonth() + 1) + '/' + end.getDate() + '/' + end.getFullYear());
});

you also have to add changing event to select element. value of end is getting from $('#start_date').val() instead of $(this).val().

Comments