Editor - Add a date to a field when entering data in another field

Editor - Add a date to a field when entering data in another field

musicawaysmusicaways Posts: 5Questions: 3Answers: 0
edited June 2017 in Free community support

Hi guys
Could you help me?
I would like to automate some fields inside the form, in practice I would like that when I add a value in the Date1 field, the Date2 field is automatically populated by adding 10 days.
I carry the script that I currently use and that does not work properly...
Thank you very much.

      editor.dependent( 'table1.date1', function ( val, data ) {
              editor.set( 'table1.date2', (editor.get( 'table1.date1' )+10));
     } );    

Answers

  • allanallan Posts: 63,893Questions: 1Answers: 10,531 Site admin

    Hi,

    The problem with the current approach is that editor.get( 'table1.date1' ) will be returning a string (perhaps 'Monday 12th June' or whatever). Doing '+10' is just concatenating the 10 to the string.

    Are you using MomentJS on the page already to format the dates? If so, its date manipulation methods are the way to do it. Parse the date string into Moment, manipulate it and then format it back out into date2.

    The basic approach you have is correct - its just missing that step.

    Allan

This discussion has been closed.