1

I have a form that tracks its dirty state by listening to the onChange events of all its form fields. If the form is dirty, the user is warned about unsaved changes when he tries to leave the page or tries to select another item.

This works well with the form in a "normal page".

This form is also used inside an iframe. When changing the value of the first input field and then jumping to another field in the form, the onChange event is triggered as expected and the form is set dirty. But when only one input field is changed and you click anywhere outside the iframe, the onChange event of the changed input field is not triggered. It seems as if the input field does not really loses its focus, when the iframe loses it.

How can I make sure that the onChange event is fired when clicking outside the iframe?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
James
  • 11,654
  • 6
  • 52
  • 81
  • Please can you provide your piece of code ? – Corinne Kubler Oct 17 '12 at 19:45
  • Did you try to make a listener for click event int the iframe, and another on the input. In the input event callback, just call the stopPropagation. So now you can detect click made on the input(the focus), and click makde outside (blur) – JuSchz Oct 17 '12 at 19:52
  • This was a timing issue. When clicking outside the iframe, the iframe gets updated by the outer frame and the "dirty form" inside the iframe dissapears without having enough time to raise the onchange event. When i put the call that updates the iframe in a setTimeout and only run it after 300ms, the form has enough time to trigger the onchange event and set the form dirty. Is there any chance that I can determine when it is possible to update the iframe so that the form elements had enough time to trigger any onchange event? – James Oct 18 '12 at 12:24
  • Does this answer your question? [How do I programmatically force an onchange event on an input?](https://stackoverflow.com/questions/136617/how-do-i-programmatically-force-an-onchange-event-on-an-input) – Brian Tompsett - 汤莱恩 Mar 24 '20 at 17:42

1 Answers1

0

You'll probably have to check to see if the iframe's window.blur event triggered then check to see if the then use JS to trigger the onchange event

How do I programmatically force an onchange event on an input?

Community
  • 1
  • 1
hobberwickey
  • 6,118
  • 4
  • 28
  • 29