Skip to content

Instantly share code, notes, and snippets.

@patrickfreitasdev
Last active December 9, 2023 03:39
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save patrickfreitasdev/1a366d2377a4701c5128120b12da453a to your computer and use it in GitHub Desktop.
<?php
add_action('wp_footer', function () {
global $post;
if (!$post instanceof WP_Post || !has_shortcode($post->post_content, 'forminator_form')) {
return;
}
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
setTimeout(function() {
$('.forminator-custom-form').trigger('after.load.forminator');
});
$(document).on('after.load.forminator', function(event, form_id) {
let _form_id = 105,
_date_field_id1 = 'date-1',
_date_field_id2 = 'date-2',
_days_field_id = 'number-1', //or enter text-field id
_separated_date = '/',
_form = $('#forminator-module-' + _form_id);
if (_form.length) {
let _date_field1 = _form.find('[name="' + _date_field_id1 + '"]'),
_date_field2 = _form.find('[name="' + _date_field_id2 + '"]');
// _default_value1 = _date_field1.val(),
// _default_value2 = _date_field1.val();
_date_field1.on('change', function() {
let _value = $(this).val();
if (_value) {
_date_field2_value = _date_field2.val();
if (_date_field2_value) {
calc_days(_value.split(_separated_date), _date_field2_value.split(_separated_date), $(this).data('format').split(_separated_date));
}
}
});
_date_field2.on('change', function() {
let _value = $(this).val();
if (_value) {
_date_field1_value = _date_field1.val();
if (_date_field1_value) {
calc_days(_date_field1_value.split(_separated_date), _value.split(_separated_date), $(this).data('format').split(_separated_date));
}
}
});
function calc_days(_date_value_1, _date_value_2, _date_format) {
let _date1 = {},
_date2 = {};
for (let _t in _date_format) {
_date1[_date_format[_t]] = _date_value_1[_t];
_date2[_date_format[_t]] = _date_value_2[_t];
}
// Fill age field.
_date1 = new Date(_date1['yy'], _date1['mm'], _date1['dd']);
_date2 = new Date(_date2['yy'], _date2['mm'], _date2['dd']);
_form.find('[name="' + _days_field_id + '"]').val(Math.round(Math.abs(_date2 - _date1) / 86400000));
}
}
});
});
</script>
<?php
}, 21);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment