Hotkey to toggle checkboxes does opposite
        Posted  
        
            by 
                Joel Harris
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Joel Harris
        
        
        
        Published on 2012-12-11T16:44:09Z
        Indexed on 
            2012/12/11
            17:03 UTC
        
        
        Read the original article
        Hit count: 373
        
JavaScript
|jQuery
In this jsFiddle, I have a table with checkboxes in the first column. The master checkbox in the table header functions as expected toggling the state of all the checkboxes in the table when it is clicked.
I have set up a hotkey for "shift-x" to toggle the master checkbox. The desired behavior when using the hotkey is:
- The master checkbox is toggled
 - The child checkboxes each have their checked state set to match the master
 
But what is actually happening is the opposite...
- The child checkboxes each have their checked state set to match the master
 - The master checkbox is toggled
 
Here is the relevant code
$(".master-select").click(function(){
    $(this).closest("table").find("tbody .row-select").prop('checked', this.checked);
});
function tickAllCheckboxes() {
  var master = $(".master-select").click();
}
//using jquery.hotkeys.js to assign hotkey
$(document).bind('keydown', 'shift+x', tickAllCheckboxes);
This results in the child checkboxes having the opposite checked state of the master checkbox. Why is that happening? A fix would be nice, but I'm really after an explanation so I can understand what is happening.
© Stack Overflow or respective owner