wip adding date to class was a mistake since it is global

This commit is contained in:
asus
2024-04-23 13:11:10 +02:00
parent 5553c78b21
commit 1447fdbc8e
3 changed files with 260 additions and 57 deletions

View File

@@ -3,30 +3,36 @@
* -> https://stackoverflow.com/questions/26667720/how-to-get-the-selected-date-from-jquery-datepicker
* then create an hidden input with the date in acf format 'yymmdd'
*
* another solution would be to find the elements with the datepicker, and add the onSelect here
*
*/
// store the original jQuery UI datepicker function
const original_datepicker = jQuery.fn.datepicker;
// override the datepicker function
jQuery.fn.datepicker = function(options) {
/*
* first get the target element and create the hidden input
*
*/
const hidden_id = 'acf_date_hidden_' + this.attr('id');
const hidden_name = 'acf_date_hidden_for_' + this.attr('name');
if (jQuery('#' + hidden_id).length === 0) {
jQuery(`<input type="hidden" id="${hidden_id}" name="${hidden_name}">`).insertAfter(this);
}
/*
* then override the options to add action on select date :
* -> store the date in acf format in the hidden field value
*
*/
options.onSelect = function(date_text, inst) {
const acf_date_format = "yymmdd";
const selected_date = jQuery(this).datepicker('getDate');
console.log("-- selected_date:", selected_date);
const formated_date = jQuery.datepicker.formatDate(acf_date_format, selected_date);
const instance_element = document.getElementById(inst.id);
console.log("instance_element:", instance_element);
const hidden_field_id = inst.id + '_acf_date_hidden';
if (document.getElementById(hidden_field_id) === null) {
var acf_date = document.createElement("input");
acf_date.setAttribute('type', 'hidden');
acf_date.setAttribute('id', hidden_field_id);
acf_date.setAttribute('name', 'acf_date_hidden_for_' + instance_element.name);
instance_element.insertAdjacentElement('afterend', acf_date, acf_date);
}
else {
var acf_date = document.getElementById(hidden_field_id);
}
acf_date.value = formated_date;
const hidden_id = 'acf_date_hidden_' + inst.id;
jQuery('#' + hidden_id).val(formated_date);
}
// call the original datepicker function with the updated option