I have a problem with (un)checking a checkbox using a keydown and combinating it with mouse click. I have a checkbox with id "check" and heres a part of my jQuery code that is checking and unchecking my checkbox by hitting the space bar:
$(document).keydown(function (e) {
if (e.keyCode == 32) {
if ($("#check").is(":checked")) {
$("#check").prop("checked", false);
}
else {
$("#check").prop("checked", true);
}
}
});
Everything works nice when document is loaded. But when I check or uncheck checkbox just by classic mouse click I can't use space bar for (un)checking anymore. Everytime I hit spacebar (after I already clicked on checkbox) checkbox checks and unchecks quickly. For example when it's checked and its in this broken state, hitting space bar results only to checbox being checked again.
Thanks for every idea how to prevent this undesirable behavior.