foxybagga
foxybagga

Reputation: 4214

Change Date in jQuery Datepicker with the Selection of the Other

I am creating a form with "Registration Date", "Renewal Date" and "Valid Upto" fields in it.

The Renewal Date is 3+ Years and Valid Upto is 3+Years and 2+Months

I wanted the Renewal Date and Valid Upto Date to populate automatically on selection of the Registration Date.

Below is the code I used to attempt this ...

    var datePicked = function() {
    var newDate = $("#reg_date").datepicker('getDate'),
    newDay = newDate.getDate(),
    newMonth1 = newDate.getMonth(),
    newMonth = newDate.getMonth()+2,
    newYear = newDate.getFullYear()+3;

    var renDate = new Date(newYear, newMonth1, newDay);
    var validDate = new Date(newYear, newMonth, newDay);

    $("#ren_date").datepicker("setDate", renDate);
    $("#valid_date").datepicker("setDate", validDate);

}

$(function() {      
    $( "#reg_date" ).datepicker({
        changeMonth: true,
        changeYear: true,
        yearRange: '1900:2050',
        dateFormat: 'dd/mm/yy',
        onSelect: datePicked
    });

    $( "#ren_date" ).datepicker({
        changeMonth: true,
        changeYear: true,
        yearRange: '1900:2050',
        dateFormat: 'dd/mm/yy'
    });     

    $( "#valid_date" ).datepicker({
        changeMonth: true,
        changeYear: true,
        yearRange: '1900:2050',
        dateFormat: 'dd/mm/yy'          
    });     

}); 

Cheers!

Upvotes: 0

Views: 623

Answers (1)

Punit Rathore
Punit Rathore

Reputation: 970

You can use the jQuery "change" event to set the other dates whenever the Registration date is selected.


$("#reg_date").change(function(){
  var reg_date_val = $(this).val();
  $("#ren_date").val(reg_date_val);
  $("#valid_date").val(reg_date_val);
});

Upvotes: 1

Related Questions