If you expect this rather undesirable behavior, then one of them will pass an additional parameter from jQuery.trigger () to the click handler. This additional parameter is intended to notify the click handler who clicked programmatically, and not by the user, by directly clicking on the check box. Then the click handler can invert the registered check status.
So, here is how I can trigger the click event on the checkbox with the id "myCheckBox". Note that I also pass a parameter to the object with one member, nonUI, which is set to true:
$("#myCheckbox").trigger('click', {nonUI : true})
And this is how I handle this in the checkbox event handler. The handler function checks for a nonUI object as the second parameter. (The first parameter is always the event itself.) If the parameter is present and set to true, I will invert the registered .checked status. If such a parameter is not passed - which will not happen if the user simply clicks on the checkbox in the user interface - then I report the current .checked status:
$("#myCheckbox").click(function(e, parameters) { var nonUI = false; try { nonUI = parameters.nonUI; } catch (e) {} var checked = nonUI ? !this.checked : this.checked; alert('Checked = ' + checked); });
JSFiddle version at http://jsfiddle.net/BrownieBoy/h5mDZ/
I tested Chrome, Firefox, and IE 8.
ChillyPenguin Jun 21 2018-12-12T00: 00Z
source share