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: 283

Filed under:
|

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:

  1. The master checkbox is toggled
  2. The child checkboxes each have their checked state set to match the master

But what is actually happening is the opposite...

  1. The child checkboxes each have their checked state set to match the master
  2. 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

Related posts about JavaScript

Related posts about jQuery