How to Check All Checkboxes Using jQuery

, , Leave a comment

Check all and uncheck all check boxes is a pretty common functionality in web development projects. Previously we used to do this functionality using javascript. In this article I will explain How to implement check all and uncheck all check boxes using jQuery.

We will start with the mark up

HTML

{code}<form>

<div class=”infobox”>
<input type=”checkbox” id=”checkall”>Check/Uncheck all Checkboxes
</div>

<div class=”infobox”>
<input type=”checkbox” id=”pizza” name=”pizza” value=7>Pizza $7
</div>

<div class=”infobox”>
<input type=”checkbox” id=”pasta” name=”pasta” value=6>Pasta $6
</div>

<div class=”infobox”>
<input type=”checkbox” id=”hotdog” name=”hotdog” value=3>HotDog $3
</div>

<div class=”infobox”>
<input type=”checkbox” id=”coke” name=”coke” value=1>Coke $1
</div>

<div class=”infobox”>
<input type=”checkbox” id=”fries” name=”fries” value=3>French Fries $3
</div>

</form>{/code}

Javascript

Dowonload jquery-1.4.4.min.js

I have created a  “check.js” file into “js” folder. All of our javascript code will go into this javascript file.
{code}<script src=”js/jquery-1.4.4.min.js” type=”text/javascript”></script>

<script src=”js/check.js” type=”text/javascript”></script>{/code}
We already create the “check.js” file.  Now we are going to add our javascript into the “check.js” file. Add the following code into check.js file.
{code}$(document).ready(function() {

$(‘#checkall’).click(function(){

$(“input[type=’checkbox’]”).attr(‘checked’, $(‘#checkall’).is(‘:checked’));

});

 

//——calculate the value———

$(‘form’).find(‘:checkbox’).click(function(){

var amt=0;

$(‘div’).filter(‘:gt(0)’).find(‘:checkbox’).each(function(){

if($(‘div:gt(0)’))

{

if($(this).is(‘:checked’))

{

amt=amt+parseInt($(this).val());

}

}

});

$(‘p’).remove();

$(‘<p>’).insertAfter(‘div:eq(5)’);

$(‘p’).text(‘Your bill is $ ‘+amt);

});

});{/code}
The .attr() method is used for setting the attributes of the selected element(s).

Syntax:

.attr(attribute, value)

The .is() method checks the selected element(s) with a selector and returns true if the any of the selected elements match with the selector; otherwise it returns false.

Syntax:

.is(selector)

The $(#checkall).is(:checked) part of the preceding statement checks if the checkbox of id:checkall is checked. If the checkbox (id: checkall) is checked, the .is() method will return true; otherwise it will return false.

If the .is() method returns true, all the input elements of type:checkbox (that is, all the checkboxes) are set to checked mode; all of them will be set to unchecked mode if the .is() method returns false.

Since the user is allowed to check any individual check box, we check the status of each checkbox that has an index value greater than 0 (because the checkbox with index value 0 is the Check All checkbox).

The value of all the checkboxes is added to the amt variable. To display the bill, we create a paragraph element and add the text Your bill is amt (where amt is the numerical value contained in the amt variable), and insert this paragraph element after the div element of index value 5; that is, after the last checkbox.

Styling with CSS

{code}body{font-family:Verdana, Geneva, sans-serif; font-size:12px;}

.infobox{ padding: 5px; }{/code}

See the Demo

Share this: