Check All/None Checkboxes Using MooTools

By  on  

There's nothing worse than having to click every checkbox in a list. Why not allow users to click one item and every checkbox becomes checked? Here's how to do just that with MooTools 1.2.

The XHTML

<table>
	<tr>
		<th width="30"><img src="checkboxes/uncheck.jpg" id="ucuc" /></th><th>Title</th>
	</tr>
	<tr>
		<td><input type="checkbox" name="approve[]" class="check-me" value="Braveheart" /></td>
		<td>Braveheart</td>
	</tr>
	<tr>
		<td><input type="checkbox" name="approve[]" class="check-me" value="Gladiator" /></td>
		<td>Gladiator</td>
	</tr>
	<tr>
		<td><input type="checkbox" name="approve[]" class="check-me" value="The Patriot" /></td>
		<td>The Patriot</td>
	</tr>
	<tr>
		<td><input type="checkbox" name="approve[]" class="check-me" value="300" /></td>
		<td>300</td>
	</tr>
</table>

Note the image with the ucuc ID -- that image will be the checkbox trigger.

The CSS

#ucuc	{ cursor:pointer; }

Give the image the pointer cursor for enhanced usability.

The MooTools 1.2 JavaScript

window.addEvent('domready', function() {
	var ucuc = $('ucuc');
	ucuc.addEvent('click', function() {
		if(ucuc.get('rel') == 'yes') {
			do_check = false;
			ucuc.set('src','checkboxes/uncheck.jpg').set('rel','no');
		}
		else {
			do_check = true;
			ucuc.set('src','checkboxes/check.jpg').set('rel','yes');
		}
		$$('.check-me').each(function(el) { el.checked = do_check; });
	});
});

Extremely simple: read in whether to check or uncheck the boxes, take the appropriate action, and swap out the images.

Recent Features

  • By
    CSS @supports

    Feature detection via JavaScript is a client side best practice and for all the right reasons, but unfortunately that same functionality hasn't been available within CSS.  What we end up doing is repeating the same properties multiple times with each browser prefix.  Yuck.  Another thing we...

  • By
    From Webcam to Animated GIF: the Secret Behind chat.meatspac.es!

    My team mate Edna Piranha is not only an awesome hacker; she's also a fantastic philosopher! Communication and online interactions is a subject that has kept her mind busy for a long time, and it has also resulted in a bunch of interesting experimental projects...

Incredible Demos

  • By
    Animated Progress Bars Using MooTools: dwProgressBar

    I love progress bars. It's important that I know roughly what percentage of a task is complete. I've created a highly customizable MooTools progress bar class that animates to the desired percentage. The Moo-Generated XHTML This DIV structure is extremely simple and can be controlled...

  • By
    Build a Toggling Announcement Slider Using MooTools 1.2

    A few of my customer have asked for me to create a subtle but dynamic (...I know...) way for them to advertise different specials on their website. Not something that would display on every page, but periodically or only the homepage. Using a trick...

Discussion

  1. andrei009

    you can do that more simple

    $('ucuc').addEvent('click',function(){
        do_check = $(this).checked;
        $$('.check-me').each(function(el) { el.checked = do_check; });
    });
    
  2. $$('.check-me').each(function(el) { el.checked = do_check; });  
    

    could be

    $$('.check-me').set('checked',do_check)  
    

    Since all Element methods can be applied over instances of Elements class ($$ returns it). Those methods apply loops internally (see Elements.multi)
    :)

  3. Thanks for the tips guys!

  4. jorge

    it would be interesant do this example with pagination, keeping the marked selection during page navigation. It could be with cookies…

  5. Finally someone who can make check all work with a name=”xyz[]”. Only issue is I tried to input your code and got error: “object doesn’t support this property or method” and “style is null or not an object”.

    What did I miss here?

  6. Farao

    Very nice work!

    One question: what would you do if you have multiple tables, each inside its own form, and you want to check/uncheck only the checkboxes inside each particular table?

  7. Adam

    Just incase anybody wants to do it without putting class names on all your checkboxes. Instead, stick an id on a html element that surrounds them all.
    This is probably the answer to “Farao’s” question.

    Set var yourContainer to be the id of the surrounding html element.
    Example, if your container had an id of “monkey” then var yourContainer = “#monkey”. This way it only targets checkboxes in that area. It’s a far better global solution than putting a class on each checkbox.

    This method takes the first checkbox found and uses it as a reference for the others to follow, to avoid mismatch checking.

    $$(yourContainer+" input[type=checkbox]").each(function(el,c){
        this.cbp = c==0?(el.checked?false:true):this.cbp;
        el.checked = this.checkMe;
    });
    

    Simplez.

  8. @Adam:
    My code above is flawed,ignore it, or David, please delete it :)

    Here’s the code that works (shorter too!)

    @param container => The mootools representation of the element id that all the checkboxes are in. Eg “#myElementId”
    @param obj => Set to ‘this’. That’s because the onclick event should be on the checkbox that is the SelectAll/None, so ‘this’ refers to the current state of this checkbox.

    function toggleCheckboxes(container,obj){
    	$$(container+" input[type=checkbox]").each(function(el){
    		el.checked = obj.checked;
    	});
    }
  9. You should write $$(“input[type$=checkbox]”)… tra-la-la

    • Good suggestion, but that may get elements I don’t want, so maybe:

      $$(“input[type$=checkbox].check-me“)

      This post is quite old, maybe I should update it.

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