Fire onChange when a changed field has not lost focus, but form was saved

1k views Asked by At

CRM 2015 (and possibly other versions) seems to have a bug as follows:

  1. A field is registered with an onChange handler.
  2. The user changes this field, and:
  3. The user immidiately clicks save. That is, before clicking the save button, the user neither presses the Enter key nor clicking somewhere on the form to explicitly loose focus.

The result is that the onChange handler of this field is not fired.

What can be done to fire the unfired onChange handler in a supported way?

3

There are 3 answers

0
OfirD On BEST ANSWER

I was wrong, onChange does fire.

3
mplungjan On

Call it if it exists:

document.getElementById("field1").onchange=function() { console.log("change handler called") }

document.getElementById("form1").onsubmit=function() {
   this.querySelectorAll("input").forEach(function(fld) {
     if (fld.onchange) fld.onchange();
   })  
}
<form id="form1">
<input type="text" id="field1" />
<input  type="submit" />
</form>

1
Arun Vinoth-Precog Tech - MVP On

Good catch. This should be resolved by user training. Otherwise we have to circumvent by trapping & completing the preventDefault of onSave event, check isDirty of attributes, then fireOnchange and then explicit save event trigger.

As the documentation says focus has to lose for onChange to trigger:

The OnChange event occurs in the following situations:
• Data in a form field has changed and focus is lost.