geetarista (owner)

Fork Of

gist: 71378 by henrik jQuery checkbox utilities: ...

Revisions

  • a5014e Fri Feb 27 01:41:23 -0800 2009
gist: 73884 Download_button fork
public
Public Clone URL: git://gist.github.com/73884.git
Embed All Files: show embed
jquery.checkboxes.js #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// jQuery checkbox utility plugin.
// By Henrik Nyh <http://henrik.nyh.se> 2009-02-27 under the MIT License.
 
(function($) {
 
  // Check a range of boxes by clicking one, then another while holding Shift or Command.
  // Code from http://www.barneyb.com/barneyblog/projects/jquery-checkbox-range-selection/
  // modified to support Command and so it can be reapplied after JS re-sort without issues.
  // $('#container :checkbox').enableCheckboxRangeSelection();
  $.fn.enableCheckboxRangeSelection = function() {
    var lastCheckbox = null;
    var $spec = this;
    $spec.unbind("click.checkboxrange");
    $spec.bind("click.checkboxrange", function(e) {
      if (lastCheckbox != null && (e.shiftKey || e.metaKey)) {
        $spec.slice(
          Math.min($spec.index(lastCheckbox), $spec.index(e.target)),
          Math.max($spec.index(lastCheckbox), $spec.index(e.target)) + 1
        ).attr({checked: e.target.checked ? "checked" : ""});
      }
      lastCheckbox = e.target;
    });
    return $spec;
  };
 
  // $(':checkbox').checkAll();
  $.fn.checkAll = function() {
    return $(this).attr('checked', true);
  };
  
  // $(':checkbox').uncheckAll();
  $.fn.uncheckAll = function() {
    return $(this).attr('checked', false);
  }
  
  // Master checkbox that can check/uncheck other checkboxes:
  // $('#slaves :checkbox').checkAllWith('#master:checkbox');
  jQuery.fn.checkAllWith = function(master) {
    $slaves = $(this);
    $master = $(master);
 
    // If the control box is initially checked, check the others, but not the other way around.
    if (!!$master.attr('checked')) {
      $slaves.attr('checked', true);
    }
 
    $master.click(function() {
      $slaves.attr('checked', !!$master.attr('checked'));
    });
    return $slaves;
  };
 
})(jQuery);