My apologies if this was asked before, but I did not find an answer searching.
I am calling .click() on a checkbox from an outside source.
The issue at hand is that the ".click" action takes place prior to it clicking the checkbox, instead of afterwards. This prevents any "if(this.checked)" calls to fail, as what it is during the click command is the opposite of what it will be afterwards. If I reversed it, then the real action of clicking on the checkbox would be wrong.
Below is probably the easiest example I can give, to clarify the above:
<input type="checkbox" id="cb" onclick="if(this.checked){$('#Table').hide(400);}else{$('#Table').show(400);};" />
<input type="button" onclick="$('#cb').click()" />
If you click the checkbox and it becomes checked, the table hides. If it becomes unchecked, it show.
If you click the button and the checkbox becomes checked, the table shows. If you click the button and the checkbox becomes unchecked, the table hides.
My temporary fix for this is the following. It does work, but seems a bit much for something so simple.
<input type="button" onclick="$('#cb').click().bind('click.tmp', function(event){event.preventDefault()}).click().unbind('click.tmp');" />
Steps taken:
Click box, tests fail
Bind function to prevent default
Click box, test succeeds, function prevents unchecking/checking
Unbind function which prevents the default
Any cleaner solutions?