I have a form with several input fields.
If one of the inputs of the form was changed, I would like to toggle the css class of the form, so that I can display a text like "Dear user, please submit the form now".
Optional, but would be nice: if the user changes the input field to the initial value again, then the class should be toggled back again, so that I can hide the text (via css)
I don't need to support IE, just modern browsers.
CSS for the text could look like this:
form.unchanged div.submit-text {
display: hidden;
}
form.changed div.submit-text {
display: block;
}
I could do it like this:
function initChangeDetection(form) {
Array.from(form).forEach(el => el.dataset.origValue = el.value);
}
function formHasChanges(form) {
return Array.from(form).some(el => 'origValue' in el.dataset && el.dataset.origValue !== el.value);
}
Source: https://stackoverflow.com/a/52593167/633961
But this creates too much load at the client.