We help IT Professionals succeed at work.

Checkbox reset when onClick handler is invoked with dispatchEvent

Running the following jsfiddle:
  https://jsfiddle.net/xrnz5tjx/

I set a input checkbox to "true" when clicking on a link, and then programatically trigger the onClick handler for the checkbox field.
In Chrome, all is well but in IE11 the checkbox is reset to false when the onClick handler is executed.

Any ideas on why IE does this?
Comment
Watch Question

Author

Commented:
I just tested with changing the checkbox to be "checked" to begin with, anticipating that it would retain that checked state in the onclick handlier in IE
However, the checkbox is still "false" even then, using IE. Strange?
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
The problem is that you have this
<a href="javascript:document.F.ClickField.checked = true; /*ClickTest();*/FireEvent(document.F.ClickField, 'click');">Click</a>

Open in new window

The first bit is setting the checkbox ticked and then in the FireEvent you are sending the onclick which is unticking the box.

It looks like a timing issue between the two browsers that is resulting in a different result.

Could I ask what it is you are trying to do - it seems rather involved and possibly could be done a lot easier.

Author

Commented:
Aha, you mean that the onclick handler actually performs the action of ticking/unticking the checkbox as well?
Well ok, of course!

It's a little strange, then, that the checkbox ends up "false" regardless of initial state.
One would guess that onclick would toggle the state, not reset it?
Most Valuable Expert 2017
Distinguished Expert 2019
Commented:
No it makes perfect sense.

You have
javascript:document.F.ClickField.checked = true

Open in new window

Which says set checkbox true. If false to begin with it is checked, if true to begin with - still checked.

No you fire a click against a true (checked) checkbox - irrespective of initial state - checks the box off - at least in IE anyway - which intuitively is what you would expect.

Author

Commented:
So, one could say that Chrome (and Firefox and Safari) is at error here - and IE does the right thing?
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
I am reluctant to give IE credit for anything - they usually get the interpretation wrong and I would need to do more research into this to find out why the behaviour is as it is  - there might be a good reason for it.