Here is my HTML:
<input class='user_roles' id="chk" type=checkbox />
<input id="btn_on" type="button" value="on" />
<input id="btn_off" type="button" value="off" />
And my jQuery:
$('#chk').click(function() { if($(this).prop("checked") === true) { alert ("checked"); } else{ alert ("not checked"); } }); $("[id$=btn_on]").click(function () { $('#chk').click(); }); $("[id$=btn_off]").click(function () { $('#chk').click(); });
A working example is here .
When you click on a checkmark with the mouse, it is checked immediately - that is, the warning is displayed as "checked". However, when you click one of the buttons that indirectly calls the click method on a flag, this flag is not pressed until the click code is executed.
To check this, check the box several times with the mouse and you will see the warnings "checked" and "not checked" respectively. Click "On" and you will see a warning "not verified", and then check the box. If you then click โOff,โ you will see โchecked,โ and then check the box.
What is the reason for this behavior? How can I make sure that the check box occurs before the click listener code is called according to the behavior of the mouse?
Andy f
source share