Skip to content

InputfieldDatetime does not trigger change event on <input> #2062

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
BernhardBaumrock opened this issue Apr 14, 2025 · 3 comments
Open

InputfieldDatetime does not trigger change event on <input> #2062

BernhardBaumrock opened this issue Apr 14, 2025 · 3 comments

Comments

@BernhardBaumrock
Copy link

I'm working on an auto-save feature for my current project. I check for changes in the page editor like this:

document.addEventListener("input", (e) => this.registerChange(e.target));
document.addEventListener("change", (e) => this.registerChange(e.target));

Some fields like TinyMCE need special treatment, but I also realised that date inputs also do not trigger a save and I was wondering why.

This is the fix that makes it work:

// Monitor datepicker changes
$(document).on("change", ".InputfieldDatetimeDatepicker", (e) =>
  this.registerChange(e.target)
);

But I think it would be better to also trigger the change event in InputfieldDatetime.js:

if (pickerVisible) {
	$datepicker.on("change", function (e) {
		var d = $datepicker.datepicker("getDate");
		var str = $.datepicker.formatDate(dateFormat, d);

		// add .trigger('change') here
		$t.val(str).trigger("change");
	});
}
@ryancramerdesign
Copy link
Member

@BernhardBaumrock I can't see to duplicate that. I edited a page with an InputfieldDatetime field using the datepicker and then typed this into the browser dev tools js console:

$('.InputfieldDatetimeDatepicker').on('change', function() { console.log('change') });

Every time I change the value in the datepicker, I get a "change" in the console, whether selecting from the datepicker or typing the value in the input.

@BernhardBaumrock
Copy link
Author

Hey @ryancramerdesign thx!

This is because jQuery has a totally custom implementation of event handling: jquery/jquery#4815 (comment)

So a jQuery event listener works:

$('#Inputfield_magicfrom').on('change', () => console.log('jQuery change'));

Image

But a native (vanilla) JS event listener doesn't:

document.addEventListener('change', () => console.log('native change'));

Try to edit any other field (like a select or a regular input). There it will fire.

I'm thinking... maybe it's best to stick to jQuery event handling then with all my backend js code?

@teppokoivula
Copy link

From jQuery UI docs (https://api.jqueryui.com/datepicker/):

This widget manipulates its element's value programmatically, therefore a native change event may not be fired when the element's value changes.

The non-standard/custom event handling mechnanism in jQuery does indeed mean that, in my experience, in e.g. our admin it is better to rely on jQuery .on() than JS event listeners.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants