jQuery shiftcheckbox plugin

Presenting a jQuery plugin to select a range of consequetive checkboxes with just two clicks. The plugin is inspired from the GMail checkboxes functionality and works exactly the same. Just keep the ‘Shift’ key pressed and select a starting checkbox and the last checkbox that you want to select. Just try the demo if things are not yet clear

Click here for a demo.

Usage

Include the plugin file in your page after jQuery file, assign similar CSS class names to the checkboxes to group them and call the plugin as follows

  1. $(document).ready (
  2.         function () {
  3.                $('.css-class-name').shiftcheckbox();
  4.         }
  5. );

You can have as many checkbox groups as you want on the same page as long as each group has a unique CSS class name attached to it.

Download here.

30 Responses to jQuery shiftcheckbox plugin

  1. Anders July 6, 2009 at 8:45 pm #

    The script dosent work.
    Tested on FF 3.5 and latest Safari (both on Mac)

    • Tarique Sani July 6, 2009 at 10:31 pm #

      The script does not work in your setup OR the demo listed on this page is also not working?

  2. Anders July 6, 2009 at 10:37 pm #

    Sorry, it works great !!
    It was me who screwed up.

    Great work !

  3. Ivan August 5, 2009 at 2:45 pm #

    Thanks! Great plugin!

  4. Nitesh September 26, 2009 at 11:56 am #

    hey gr8 tool saved my life. :) thanx a million…

    Regards
    Nitesh Chauhan.

  5. Stefan May 31, 2010 at 12:42 pm #

    It would be great if it would work with unchecking also,

    for instance, click 1st with shift then 10th to check all inn between, then click 3rd and 8th with shift to uncheck 3 through 8.

    • Uli July 5, 2010 at 4:52 pm #

      Stefan, it does what you want. All you need to do is “break the chain”. You do this by releasing the shift key, deselecting the first checkbox in the range you want deselected (in your example the “3″) and then reselecting it again. Not until then you press shift again and click the last in the row that shall be deselected: voilà!

      Thanks, Aditya!

  6. 123doing June 4, 2010 at 8:25 am #

    It’s very good.
    I like this.
    Thanks for share.
    And I wrote something to introduce this project for my readers.
    You can find the post about this in my website.
    If something is wrong,pls figure it out.thanks.

  7. Azan August 5, 2010 at 9:23 pm #

    How to use shiftcheckbox with jquery.tablesorter.pager.js?
    Thanks!

  8. Uli August 9, 2010 at 1:55 am #

    Hi Aditya,

    why did you prefer the class selector over “input[type=checkbox”? I just tried to implement the plugin in the admin of my CMS and remarked input[type=checkbox works. Will it have any disadvantages?

    Thanks again!
    Uli

  9. Uli August 9, 2010 at 2:18 am #

    Forgetful me!
    You can have as many checkbox groups as you want on the same page as long as each group has a unique CSS class name attached to it.

    • Aditya Mooley August 9, 2010 at 8:24 am #

      Yes, that’s the reason for adding class names :)

  10. Mårten October 19, 2010 at 5:24 pm #

    Hi, I added support for a “check all” checkbox. This check all could be in the column header if the shiftcheckboxes are in a table.

    Just call $(‘.’).shiftcheckbox(‘#) in $(document).ready


    (function($) {
    $.fn.shiftcheckbox = function(checkAllId) {
    var prevChecked = null;

    selectorStr = this;
    allCbId = checkAllId;
    $(selectorStr).bind("click", handleClick);
    if (allCbId != null) {
    $(allCbId).bind("click", handleAllClick);
    }

    };

    function handleAllClick(event) {
    var checkStatus = this.checked; // set all the checkboxes as selected
    $(selectorStr).each(function() {
    this.checked = checkStatus;
    })
    }

    function handleClick(event) {
    var val = this.value;
    var checkStatus = this.checked;
    var allChecked = checkStatus;
    //get the checkbox number which the user has checked

    //check whether user has pressed shift
    if (event.shiftKey) {
    if (prevChecked != 'null') {
    //get the current checkbox number
    var ind = 0, found = 0, currentChecked;
    currentChecked = getSelected(val);

    ind = 0;

    $(selectorStr).each(function(i) {

    if (currentChecked = currentChecked && ind = prevChecked && ind <= currentChecked) {
    this.checked = checkStatus;
    }
    }
    ind++;
    allChecked = allChecked && this.checked;
    });

    prevChecked = currentChecked;

    }
    } else {
    if (checkStatus) {
    prevChecked = getSelected(val);

    }
    $(selectorStr).each(function() {
    allChecked = allChecked && this.checked;
    });
    }
    if (allCbId != null) {
    $(allCbId).attr("checked", allChecked); // set "all" checkbox to checked
    }
    };

    function getSelected(val) {
    var ind = 0, found = 0, checkedIndex;

    $(selectorStr).each(function(i) {
    if (val == this.value && found != 1) {
    checkedIndex = ind;
    return false; // break loop
    }
    ind++;
    return true;
    });

    return checkedIndex;
    };
    })(jQuery);

    Cheers!

  11. John Ardmore December 7, 2010 at 4:43 pm #

    hi, congratulations for the project

    what i want to achieve is make this script work with two ( or more ) separate groups of check-boxes !
    is it possible ?! how ?!

    thank you.

Trackbacks/Pingbacks

  1. 20 jQuery Plugins and Tutorials to Enhance Forms : Speckyboy Design Magazine - August 26, 2009

    [...] jQuery Shiftcheckbox Plugin [...]

  2. 20 jQuery Plugins and Tutorials to Enhance Forms | pc-aras - September 4, 2009

    [...] jQuery Shiftcheckbox Plugin [...]

  3. 20 jQuery Plugins and Tutorials to Enhance Forms | WEBDESIGN FAN - September 18, 2009

    [...] jQuery Shiftcheckbox Plugin [...]

  4. 16个Javascript表单事件脚本(表单验证、选择) « 幻岛|领地 - January 10, 2010

    [...] jQuery shiftcheckbox plugin [...]

  5. jQuery shiftcheckbox plugin 开源项目 - Ajax代码大全 - Java开源项目 - 表单Form - ajax表单form控件 - Java免费软件 - jQuery-shiftcheckbox-plugin - 开源网 - June 4, 2010

    [...] jQuery shiftcheckbox plugin 项目主页 |jQuery shiftcheckbox plugin  项目下载 | jQuery sh… [...]

  6. 55 jQuery Form Plugins To Download And Use | Design your way - June 30, 2010

    [...] jQuery shiftcheckbox plugin [...]

  7. 55 jQuery Form Plugins To Download And Use « qeqnes | Designing. jQuery, Ajax, PHP, MySQL and Templates - July 2, 2010

    [...] jQuery shiftcheckbox plugin [...]

  8. 55 jQuery Form Plugins To Download And Use : Free Pictures - July 29, 2010

    [...] jQuery shiftcheckbox plugin [...]

  9. 20 jQuery Plugins and Tutorials to Enhance Forms | 冷风留痕 - June 19, 2011

    [...] jQuery Shiftcheckbox Plugin [...]

  10. jQuery Plugins and Tutorials to Enhance Forms » Example Codes - July 2, 2011

    [...] The jQuery Form Plugin allows you to easily and unobtrusively upgrade HTML forms to use AJAX. The main methods, ajaxForm and ajaxSubmit, gather information from the form element to determine how to manage the submit process. Both of these methods support numerous options which allows you to have full control over how the data is submitted. Submitting a form with AJAX doesn’t get any easier than this! jQuery Shiftcheckbox Plugin [...]

  11. 20 jQuery Plugins and Tutorials to Enhance Forms | ExceptionHandle.com - November 21, 2011

    [...] jQuery Shiftcheckbox Plugin [...]

  12. » Tutorials and plugins for dealing with web forms DESIGNLANDER - January 30, 2012

    [...] Demo | Source page [...]

  13. jQuery Plugins and Tutorials to Enhance Forms | Creative Agency Delhi – Branding, Design, Digital, Print, Consultancy, Web Design – SE1 - March 6, 2012

    [...] The jQuery Form Plugin allows you to easily and unobtrusively upgrade HTML forms to use AJAX. The main methods, ajaxForm and ajaxSubmit, gather information from the form element to determine how to manage the submit process. Both of these methods support numerous options which allows you to have full control over how the data is submitted. Submitting a form with AJAX doesn’t get any easier than this! jQuery Shiftcheckbox Plugin [...]

  14. 20 jQuery Plugins and Tutorials to Enhance Forms | Jopt | Just another WordPress site - May 10, 2012

    [...] jQuery Shiftcheckbox Plugin [...]

  15. 15 Plugins y tutoriales jQuery para mejorar tus formularios - June 1, 2012

    [...] sobre otro de los campos. Todos los que se encuentren entre ambos se seleccionan automáticamente. Seleccionar varios campos Checkbox consecutivos – [...]

  16. All posts in Forms | Thiết kế web - December 14, 2012

    [...] Read MoreDemo [...]

Leave a Reply