views:

2452

answers:

5

In GMail, the user can click on one checkbox in the email list, hold down the Shift key, and select a second checkbox. The JavaScript will then select/unselect the checkboxes that are between the two checboxes.

I am curious as to how this is done? Is this JQuery or some basic (or complex) JavaScript?

+9  A: 

This is done through fairly simple javascript.

They keep track of the id of the last checked box and when when another checkbox is checked they use the shiftKey event attribute to see if shift was held while clicking the checkbox. If so they set the checked property of each checkbox in between the two to true.

To determine when a box is checked they probably use an onclick event on the checkboxes

Ben S
+9  A: 

I wrote a self-contained demo that uses jquery:

<html>
<head>
</head>
<body>
    <input type="checkbox" id="id_chk1" class="chkbox" value="1" />Check 1<br/>
    <input type="checkbox" id="id_chk2" class="chkbox" value="2" />Check 2<br/>
    <input type="checkbox" id="id_chk3" class="chkbox" value="3" />Check 3<br/>
    <input type="checkbox" id="id_chk4" class="chkbox" value="4" />Check 4<br/>
    <input type="checkbox" id="id_chk5" class="chkbox" value="5" />Check 5<br/>
    <input type="checkbox" id="id_chk6" class="chkbox" value="6" />Check 6<br/>
    <input type="checkbox" id="id_chk7" class="chkbox" value="7" />Check 7<br/>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"&gt;&lt;/script&gt;
    <script type="text/javascript">
     var lastChecked = null;

     $(document).ready(function() {
      $('.chkbox').click(function(event) {
       if(!lastChecked) {
        lastChecked = this;
        return;
       }

       if(event.shiftKey) {
                            var start = $('.chkbox').index(this);
                            var end = $('.chkbox').index(lastChecked);

           for(i=Math.min(start,end);i<=Math.max(start,end);i++) {
         $('.chkbox')[i].checked = lastChecked.checked;
        }
       }

       lastChecked = this;
      });
     });
    </script>
</body>
</html>
BC
You can call slice instead of using the for loop. It would look like this: "$('.chkbox').slice(min..., max... + 1).attr('checked', lastChecked.checked)"
Matthew Crumley
A: 

thx a lot! Very very useful!

Max
A: 

Got it from http://abcoder.com/javascript/jquery/simple-check-uncheck-all-jquery-function/. Visit this link for more details.

Javascript code

var NUM_BOXES = 10;
// last checkbox the user clicked
var last = -1;
function check(event) {
// in IE, the event object is a property of the window object
// in Mozilla, event object is passed to event handlers as a parameter
if (!event) { event = window.event }
var num = parseInt(/box\[(\d+)\]/.exec(this.name)[1]);
if (event.shiftKey && last != -1) {
var di = num > last ? 1 : -1;
for (var i = last; i != num; i += di) {
  document.forms.boxes['box[' + i + ']'].checked = true;
 }
}
  last = num;
}
function init() {
  for (var i = 0; i < NUM_BOXES; i++) {
    document.forms.boxes['box[' + i + ']'].onclick = check;
  }
}

HTML

<body onload="init()">
<form name="boxes">
<input name="box[0]" type="checkbox">
<input name="box[1]" type="checkbox">
<input name="box[2]" type="checkbox">
<input name="box[3]" type="checkbox">
<input name="box[4]" type="checkbox">
<input name="box[5]" type="checkbox">
<input name="box[6]" type="checkbox">
<input name="box[7]" type="checkbox">
<input name="box[8]" type="checkbox">
<input name="box[9]" type="checkbox">
</form></body>
adnan
A: 

Well, the post is quite old but here is a solution I've just come across: jQuery Field Plug-In

Mike