Use jquery to update 2 fields

I'm trying to allow a user, using the jquery datepicker, to select a date for an element #delDate.

I want the #paymentDate element to equal #delDate + 30 days... preferably at the time that they select the #delDate.

I'd be neat to use less than 3 or 4 hundred lines of code.  Not have to use 15 or 20 variables.  No api calls.

If it is too involved I'll just tell the customer it can't be done.

Thanks in advance.
Sheldon LivingstonConsultantAsked:
Who is Participating?
 
Rainer JeschorCommented:
Hi,
something like this?
$(document).ready(function() {
   $("#delDate, #paymentDate").datepicker();
    $("#delDate").change(function() {
        var delDate = new Date($("#delDate").val());
        var payDate = new Date();
        payDate.setDate(delDate.getDate() + 30);
        $("#paymentDate").val(getFormattedDate(payDate));
    });
});

function getFormattedDate(date) {
  var year = date.getFullYear();
  var month = (1 + date.getMonth()).toString();
  month = month.length > 1 ? month : '0' + month;
  var day = date.getDate().toString();
  day = day.length > 1 ? day : '0' + day;
  return month + '/' + day + '/' + year;
}

Open in new window


Live sample:
http://jsfiddle.net/EE_RainerJ/ubm8ushn/

HTH
Rainer
0
 
Sheldon LivingstonConsultantAuthor Commented:
Thank you Rainer... as was at my wits end...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.