Skip to content

Instantly share code, notes, and snippets.

@nathansmith
Last active December 11, 2015 12:38
Show Gist options
  • Select an option

  • Save nathansmith/4602159 to your computer and use it in GitHub Desktop.

Select an option

Save nathansmith/4602159 to your computer and use it in GitHub Desktop.

Revisions

  1. nathansmith revised this gist Jan 23, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion check_all.js
    Original file line number Diff line number Diff line change
    @@ -42,7 +42,7 @@ var check_all = (function($, window, document, undefined) {
    // Is this the master checkbox?
    if (el.hasClass('check-all')) {
    // Is it checked?
    if (el.is(':checked')) {
    if (el.prop('checked')) {
    // Check all
    others.prop('checked', true);
    }
  2. nathansmith revised this gist Jan 23, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion check_all.js
    Original file line number Diff line number Diff line change
    @@ -37,7 +37,7 @@ var check_all = (function($, window, document, undefined) {
    }

    var others = parent.find('input[type="checkbox"]').not('.check-all');
    var others_checked = others.filder(':checked');
    var others_checked = others.filter(':checked');

    // Is this the master checkbox?
    if (el.hasClass('check-all')) {
  3. nathansmith revised this gist Jan 23, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion check_all.js
    Original file line number Diff line number Diff line change
    @@ -37,7 +37,7 @@ var check_all = (function($, window, document, undefined) {
    }

    var others = parent.find('input[type="checkbox"]').not('.check-all');
    var others_checked = parent.find('input[type="checkbox"]:checked').not('.check-all');
    var others_checked = others.filder(':checked');

    // Is this the master checkbox?
    if (el.hasClass('check-all')) {
  4. nathansmith revised this gist Jan 23, 2013. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions check_all.js
    Original file line number Diff line number Diff line change
    @@ -44,23 +44,23 @@ var check_all = (function($, window, document, undefined) {
    // Is it checked?
    if (el.is(':checked')) {
    // Check all
    others.attr('checked', 'checked');
    others.prop('checked', true);
    }
    else {
    // Un-check all
    others.removeAttr('checked');
    others.prop('checked', false);
    }
    }
    // Must be a sibling checkbox
    else {
    // Are all siblings checked?
    if (others.length === others_checked.length) {
    // Check master
    master.attr('checked', 'checked');
    master.prop('checked', true);
    }
    else {
    // Un-check master
    master.removeAttr('checked');
    master.prop('checked', false);
    }
    }
    }
  5. nathansmith revised this gist Jan 23, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions check_all.js
    Original file line number Diff line number Diff line change
    @@ -55,11 +55,11 @@ var check_all = (function($, window, document, undefined) {
    else {
    // Are all siblings checked?
    if (others.length === others_checked.length) {
    // Check master checkbox
    // Check master
    master.attr('checked', 'checked');
    }
    else {
    // Un-check master checkbox
    // Un-check master
    master.removeAttr('checked');
    }
    }
  6. nathansmith revised this gist Jan 23, 2013. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion check_all.js
    Original file line number Diff line number Diff line change
    @@ -26,7 +26,7 @@ var check_all = (function($, window, document, undefined) {
    return {
    // check_all.init
    init: function() {
    // Run on DOM load, and click.
    // Run on DOM load, and click
    function determine_checked(el) {
    var parent = el.closest('dl, ol, table, ul');
    var master = parent.find('input[type="checkbox"].check-all');
    @@ -41,6 +41,7 @@ var check_all = (function($, window, document, undefined) {

    // Is this the master checkbox?
    if (el.hasClass('check-all')) {
    // Is it checked?
    if (el.is(':checked')) {
    // Check all
    others.attr('checked', 'checked');
  7. nathansmith revised this gist Jan 23, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion check_all.js
    Original file line number Diff line number Diff line change
    @@ -28,7 +28,7 @@ var check_all = (function($, window, document, undefined) {
    init: function() {
    // Run on DOM load, and click.
    function determine_checked(el) {
    var parent = el.closest('dl, table, ul');
    var parent = el.closest('dl, ol, table, ul');
    var master = parent.find('input[type="checkbox"].check-all');

    // Exit, if no "check-all" exists
  8. nathansmith created this gist Jan 23, 2013.
    78 changes: 78 additions & 0 deletions check_all.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,78 @@
    /*
    Use like this (or with <dl>, <ol>, <table>)...
    <ul>
    <li>
    <input type="checkbox" class="check-all" />
    </li>
    <li>
    <input type="checkbox" />
    </li>
    <li>
    <input type="checkbox" />
    </li>
    </ul>
    */


    // Module pattern
    var check_all = (function($, window, document, undefined) {
    // Kickoff
    $(document).ready(function() {
    check_all.init();
    });

    // Expose publicly
    return {
    // check_all.init
    init: function() {
    // Run on DOM load, and click.
    function determine_checked(el) {
    var parent = el.closest('dl, table, ul');
    var master = parent.find('input[type="checkbox"].check-all');

    // Exit, if no "check-all" exists
    if (!master.length) {
    return;
    }

    var others = parent.find('input[type="checkbox"]').not('.check-all');
    var others_checked = parent.find('input[type="checkbox"]:checked').not('.check-all');

    // Is this the master checkbox?
    if (el.hasClass('check-all')) {
    if (el.is(':checked')) {
    // Check all
    others.attr('checked', 'checked');
    }
    else {
    // Un-check all
    others.removeAttr('checked');
    }
    }
    // Must be a sibling checkbox
    else {
    // Are all siblings checked?
    if (others.length === others_checked.length) {
    // Check master checkbox
    master.attr('checked', 'checked');
    }
    else {
    // Un-check master checkbox
    master.removeAttr('checked');
    }
    }
    }

    $(document.documentElement).off('click.check_all').on('click.check_all', 'input[type="checkbox"]', function() {
    var el = $(this);
    determine_checked(el);
    });

    $('input[type="checkbox"].check-all').each(function() {
    var el = $(this);
    determine_checked(el);
    });
    }
    };
    })(jQuery, this, this.document);