Why does input need to be focused to execute OnChange ajax?

Why does input need to be focused to execute OnChange ajax?

  
This issue made me struggle for quite long time.
I have a hidden input that is filled with value from javascript and then I want to send the value to the server, by using OnChange action. I added implicit call "target.trigger('change');" in my js - but that didn't work. To check, I added "onchange" attribute with an alert - and saw that alert was popping up, which meant the event got to the input, but action was still not called.
After some time experimenting and trying to figure out something from OS javascript, I understood that the input must be focused (before or after the change event) in order for action to be executed!
So now I have "target.focus().trigger('change');", and since hidden input can not be focused - to make it work, it should be put outside of the screen instead.

But why is that so? I can't imagine any reason for this limitation.
And is there some simple or popular workaround?
Maybe some js that can be executed to send a value to the server directly without using unreliable event?
Hi Igor,

If I'm not mistaken, there is some code to keep the focus in an input across Ajax calls, and that might be the reason you are finding that behaviour.

As an option, a common pattern I see is to have two hidden widgets, an input and a button, and clicking on the hidden button to trigger whatever you want on the change. That should be more reliable.

Cheers,
Tiago Simões