How To: Check all uncheck all and invert checkboxes using jquery

To check all and uncheck all checkboxes using jquery is real fun. I tried making for my current project and shared with you for simple copy and paste elements.

.

Check DEMO before going on.

Step 1: Html setup

Let us assume that you have following checkboxes and controls.

<div class="controls">
	<span><input type="checkbox" class="checkAll" /> <b>Check All</b> <span> or 
	<span><a href="javascript:void(0);" class="invertSelection">Invert Selection</a></span>
</div>
<div class="elements">
	<span><input type="checkbox" class="cb-element" /> Checkbox 1</span>
	<span><input type="checkbox" class="cb-element" /> Checkbox 2</span>
	<span><input type="checkbox" class="cb-element" /> Checkbox 3</span>
	<span><input type="checkbox" class="cb-element" /> Checkbox 4</span>
	<span><input type="checkbox" class="cb-element" /> Checkbox 5</span>
	<span><input type="checkbox" class="cb-element" /> Checkbox 6</span>
	<span><input type="checkbox" class="cb-element" /> Checkbox 7</span>
	<span><input type="checkbox" class="cb-element" /> Checkbox 8</span>
	<span><input type="checkbox" class="cb-element" /> Checkbox 9</span>
	<span><input type="checkbox" class="cb-element" /> Checkbox 10</span>
</div>

.

Step 2: Jquery

Copy following code inside head tag of your html. (jquery included)

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js" language="javascript"></script>
	<script type="text/javascript" language="javascript">
		$( function() {
			$( '.checkAll' ).live( 'change', function() {
				$( '.cb-element' ).attr( 'checked', $( this ).is( ':checked' ) ? 'checked' : '' );
				$( this ).next().text( $( this ).is( ':checked' ) ? 'Uncheck All' : 'Check All' );
			});
			$( '.invertSelection' ).live( 'click', function() {
				$( '.cb-element' ).each( function() {
					$( this ).attr( 'checked', $( this ).is( ':checked' ) ? '' : 'checked' );
				}).trigger( 'change' );
 
			});
			$( '.cb-element' ).live( 'change', function() {
				$( '.cb-element' ).length == $( '.cb-element:checked' ).length ? $( '.checkAll' ).attr( 'checked', 'checked' ).next().text( 'Uncheck All' ) : $( '.checkAll' ).attr( 'checked', '' ).next().text( 'Check All' );
 
			});
		});
	</script>

.

All done.. Check DEMO

Cheers!

Recently Published

»

Know-How DevOps Benefits Your Business

The organizations in this current fast-paced environment need to ...

»

10 Best Prototyping Tools for UI/UX Designers

Website design for a client gets only successful if you follow their ...

»

Social Media Marketing: Strategy To Boost Ecommerce-Traffic

According to a recent study, the experts revealed that people aged ...

»

How Flutter Can Help Reduce Overall Mobile App Development Cost?

Before jumping on answering the question, let me first walk you ...

»

Which One is Better? DigitalOcean Or AWS?

Digitalocean or AWS: Picking a cloud service for your application or ...

»

Killing way to select Domain Name and Hosting

Planning to launch your online presence? We know it’s all very ...

»

7+ blogs to inspire how they made more than $200 in a day

On this page I want to go through the blogs on how they earn money ...

»

5 Best Practices to Make Responsive Web Design in 2020

According to statistics, 40.1% of the time spent on websites came ...

»

7 sites to download free stock videos for TikTok or YouTube

Many of us always wandering for free stock videos to integrate into ...