MooTools dwCheckboxes Plugin

By  on  

Update / Fix: The checkboxes will no longer toggle when the "mouseup" event doesn't occur on a checkbox.

Every morning I wake up to a bunch of emails in my Gmail inbox that I delete without reading. I end up clicking so many damn checkboxes that my finger damn near falls off. With that in mind, I've created the dwCheckboxes plugin, a MooTools-based class that allows users to click down on a checkbox and drag their mouse over other checkboxes to check or uncheck numerous checkboxes within one click.

The MooTools JavaScript

var dwCheckboxes = new Class({
	Implements: [Options],
	options: {
		elements: 'input[type=checkbox]',	//checkboxes to use
		mode: 'toggle'								//toggle|check|uncheck
	initialize: function(options) {
		//set options
		//prevent drag start in IE
		document.ondragstart = function () { return false; }
		//ensure that the elements are a Element
		this.options.elements = $$(this.options.elements);
		//manage the checkbox actions
	//a method that does whatever should be done
	manage: function() {
		var active = 0;
		this.options.elements.each(function(el) {
				'mousedown': function(e) {
					active = 1;
					el.checked = !el.checked;
				'mouseenter': function(e) {
					if(active === 1) {
						el.checked = ('toggle' == this.options.mode ? !el.checked : 'check' == this.options.mode);
				'click': function(e) {
					el.checked = !el.checked;
					active = 0; 
		var label = $$('label[for=' + el.get('id') + ']');
		if(label.length) {
			label[0].addEvent('click',function() {
				el.checked = !el.checked;
		window.addEvent('mouseup',function() { active = 0; });

There are two options for this mall plugin:

  • elements: the collection of input boxes to apply the plugins for (defaults to all checkboxes).
  • mode: one of three options: toggle, check, or uncheck (defaults to "toggle")

The MooTools Usage

//when the DOM is ready to go
window.addEvent('domready',function() {
	var togglers = new dwCheckboxes({ elements: $$('.toggle'), mode: 'toggle' });
	var checked = new dwCheckboxes({ elements: $$('.checked'), mode: 'check' });
	var unchecked = new dwCheckboxes({ elements: $$('.unchecked'), mode: 'uncheck' });

Recent Features

  • By
    Serving Fonts from CDN

    For maximum performance, we all know we must put our assets on CDN (another domain).  Along with those assets are custom web fonts.  Unfortunately custom web fonts via CDN (or any cross-domain font request) don't work in Firefox or Internet Explorer (correctly so, by spec) though...

  • By
    Create Namespaced Classes with MooTools

    MooTools has always gotten a bit of grief for not inherently using and standardizing namespaced-based JavaScript classes like the Dojo Toolkit does.  Many developers create their classes as globals which is generally frowned up.  I mostly disagree with that stance, but each to their own.  In any event...

Incredible Demos

  • By
    MooTools, Mario, and Portal

    I'm a big fan of video games. I don't get much time to play them but I'll put down the MacBook Pro long enough to get a few games in. One of my favorites is Portal. For those who don't know, what's...

  • By
    Spatial Navigation

    Spatial navigation is the ability to navigate to focusable elements based on their position in a given space.  Spatial navigation is a must when your site or app must respond to arrow keys, a perfect example being a television with directional pad remote.  Firefox OS TV apps are simply...


  1. Nice work, one problem I see though is that in FF3/Safari 3/Chrome when I click on a checkbox then drag down the list to select more and then release, everytime I mouse over the check boxes in that group they still become checked/unchecked.

    Opera/IE7 look to work as intended.

  2. @Benjamin Sterling: Good catch — I didn’t do a post-mouseup hover when testing the toggle functionality. Looking over the code, I don’t see the issue but I’m going to address it ASAP.

  3. Just an idea. Like setting visibility to layers in photoshop. If you make a first layer visible, then all the other layer that you hover on will get the same state.

    So, difference would be that the toggle would not inverse the state of each and every checkbox but only follow the first state when we clicked.

    Example: If 2nd checkbox is checked and others are not. When i click on the first checkbox and drag. I end up with all of them checked but the 2nd one unchecked.

    I hope you understood what i mean, i wanted to make it as short as possible.

  4. Clever. What advantage does this have over shift click?

  5. Now that’s an excellent class!

    Just to note: remove the console.log‘s!

  6. @Ryan: Thanks

    @Benjamin Sterling: Fixed that issue. We’re all good now.

  7. It should work on the labels as well. And there should be a tooltip or something, maybe just a yellow notice box on the side, to alert the user of this, since it’s unexpected behavior.

  8. theBagg

    What would be better would be a noticeable graphical drag and highlight box, that when dragged over labels and checkboxes it performs the function. It took me a while to actually work out you have to hit the checkbox first. Very clever though.

  9. Nice work (as usual).

    1) Regarding the comment above about errant console log statements, might I suggest the Clientcide dbug plugin? It’s super tiny, and it insulates you from accidentally publishing code with log statements. It’s never good to to leave a bunch of log statements in code, but every now and then we all miss one sooner or later. dbug makes these not throw errors in browsers without firebug.

    2) Just a little nit-pick, but its generally considered a convention in JavaScript to use upper case names for any function that is meant to be used as a constructor. I don’t know if you do this elsewhere in your code, but it’s a big help for anyone using your stuff. It’s all too easy to leave off the “new” in front of a function that isn’t.

    Sorry, don’t mean to nag…

  10. @Aaron Newton: Thanks for the tip and thank you for your work with Moo!

  11. adamnfish

    Fredrik Bränström says:

    It should work on the labels as well.

    I agree. That was the first thing i tried and I was surprised when it didn’t work. I rarely click on the actual checkbox unless I’m forced to (if the for attribute isn’t set)

  12. I use prototype, not MooTools. Here is a copy with fewer options, but for prototype.

    Thanks, David.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!