Avatar of Stefan Lennerbrant
Stefan LennerbrantFlag for Sweden

asked on 

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?
HTMLJavaScriptWeb Browsers

Avatar of undefined
Last Comment
Julian Hansen
Avatar of Stefan Lennerbrant
Stefan Lennerbrant
Flag of Sweden image

ASKER

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?
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

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.
Avatar of Stefan Lennerbrant

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Stefan Lennerbrant

ASKER

So, one could say that Chrome (and Firefox and Safari) is at error here - and IE does the right thing?
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

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.
JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo