<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>

<channel>
	<title>Clowns In My Coffee &#187; Uncategorized</title>
	<atom:link href="http://clownsinmycoffee.net/category/uncategorized/feed/" rel="self" type="application/rss+xml" />
	<link>http://clownsinmycoffee.net</link>
	<description>Inanity of the most cogent sort you can find.</description>
	<pubDate>Thu, 02 Oct 2008 11:41:56 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.2</generator>
	<language>en</language>
			<item>
		<title>Adding Shift-select with jQuery</title>
		<link>http://clownsinmycoffee.net/2008/04/18/add-shift-select-with-jquery/</link>
		<comments>http://clownsinmycoffee.net/2008/04/18/add-shift-select-with-jquery/#comments</comments>
		<pubDate>Fri, 18 Apr 2008 17:48:07 +0000</pubDate>
		<dc:creator>adam</dc:creator>
		
		<category><![CDATA[Tools]]></category>

		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[nerdination]]></category>

		<category><![CDATA[closure]]></category>

		<category><![CDATA[code]]></category>

		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://clownsinmycoffee.net/?p=45</guid>
		<description><![CDATA[
 I&#8217;ve been using jQuery a bit here and there to add some (I hope) usability enhancements and for light AJAJ work.  Today I encountered a situation where I thought adding the &#8220;shift-select&#8221; feature on a longish list of checkboxes would be a good thing. This sort of feature pops up in webmail interfaces, [...]]]></description>
			<content:encoded><![CDATA[<p>
 I&#8217;ve been using <a href="http://jquery.com">jQuery</a> a bit here and there to add some (I hope) usability enhancements and for light AJAJ work.  Today I encountered a situation where I thought adding the &#8220;shift-select&#8221; feature on a longish list of checkboxes would be a good thing. This sort of feature pops up in webmail interfaces, where you tick off one box, scroll down through 750 spam messages, and then, while holding down shift on the 751st piece of spam in a row, click its checkbox to select all of the rows in between.  It turns out that adding this with jQuery is pretty elegant, so here&#8217;s the code.  I don&#8217;t for a moment think this is the best implementation of this idea, but I was struck by how concise the result was, while supporting &#8212; via a straightforward use of a <a href="http://en.wikipedia.org/wiki/Closure_(computer_science)">closure</a>, multiple instances on the same page.  For giggles, I added a feature that allows you to de-select a range, although I&#8217;m not convinced it works in an intuitive way.
</p>
<p>To use this, you&#8217;ll need jQuery (tested against 1.2.3) in your page, and a CSS selector that matches the checkboxes you want to enable shift-select on. Then call <code>$(selector).shiftSelect();</code> and you&#8217;re done.
</p>
<pre>
 jQuery.fn.shiftSelect = function() {
    var checkboxes = this;
    var lastSelected;
    jQuery(this).click( function(event) {

        if ( !lastSelected ) {
            lastSelected = this;
            return;
        }

        if ( event.shiftKey ) {
            var selIndex = checkboxes.index(this);
            var lastIndex = checkboxes.index(lastSelected);
            /*
             * if you find the "select/unselect" behavior unseemly,
             * remove this assignment and replace 'checkValue'
             * with 'true' below.
             */
            var checkValue = lastSelected.checked;
            if ( selIndex == lastIndex ) {
                return true;
            }

            var end = Math.max(selIndex,lastIndex);
            var start = Math.min(selIndex,lastIndex);
            for(i=start;i&lt;=end;i++) {
                checkboxes[i].checked = checkValue;
            }
        }
        lastSelected = this;
    });
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://clownsinmycoffee.net/2008/04/18/add-shift-select-with-jquery/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
