Toggle All Checkboxes with jQuery

Just a little snippet I worked up that may be useful to someone …

jQuery(document).ready(function($){
$('div#checkall-wrapper input[type=checkbox]').click(function(){
	if( $(this).attr('checked') ){
		$('tdiv#wraparound-targets input[type=checkbox]').attr('checked','checked');
	}else{
		$('div#wraparound-targets input[type=checkbox]').removeAttr('checked');
	}
});
});

Make sense?

Tags: ,

2 Responses to “Toggle All Checkboxes with jQuery”

  1. Chris Says:

    Nice tip!

    This is how I would have done it in your case:

    targets = $('div#wraparound-targets input[type=checkbox]');
    $(this).attr('checked') ? targets.attr('checked', 'checked') : targets.removeAttr('checked');
    
  2. George Stephanis Says:

    Yup, makes sense. I just know that a lot of people have a hard time understanding the ternary operator, so I wrote it out longhand.

Comments are closed.


Follow

Get every new post delivered to your Inbox.

Join 2,920 other followers

%d bloggers like this: