Support

Account

Home Forums ACF PRO Autocomplete end date when start date is selected in jquery date picker Reply To: Autocomplete end date when start date is selected in jquery date picker

  • I am having trouble getting this to work. I am able to enqueue the script, but I am not sure where or how to check if it is loading after ‘acf-input’ is loaded as you said.

    here is my code:

    // set end date from start date
    
      var start_date_key = 'field_55b9e422a0ec5'; // the field key of the start date
      var end_date_key = 'field_56274f2898327'; // the field key of the end date
    
      if (typeof(acf) != 'undefined') {
        // add an action for all datepicker fields
        acf.add_action('date_picker_init', function($input, args, $field) {
          // get the field key for this field
          var key = $input.closest('.acf-field').data('key');
          // see if it's the start date field
          if (key == start_date_key) {
            // add action to start date field datepicker
            $input.datepicker().on('input change select', function(e) {
              // get the selected date
              var date = jQuery(this).datepicker('getDate');
              // add 5 days to date
              date.setDate(date.getDate()+5);
              // set end date
              jQuery('[data-key="'+end_date_key+'"] input.hasDatepicker').datepicker('setDate', date);
            });
          }
        });
      }

    and in functions.php:

    add_action('acf/input/admin_enqueue_scripts', 'enqueue_date_script');
    function enqueue_date_script() {
    	wp_enqueue_script('date-picker', '/date-picker.js', array('acf-input'));
    }

    Am I missing something here? I have ensured that the field keys are correct. I am using ACF 5.3.0