Calculate Sum of ASP.NET CheckBox Values using jQuery
Posted by: Suprotim Agarwal
in Category jQuery and ASP.NET
Abstract: In this article, we will see how to add values of multiple ASP.NET Checkboxes using jQuery
In this article, we will see how to add values of multiple ASP.NET Checkboxes using jQuery. This example could be useful in scenarios where users are asked to select services they would like to avail and each service has a rate displayed – like in a webhosting service. Using jQuery, the total service cost should be automatically calculated, as the services are selected.
Note: If you are using jQuery with ASP.NET Controls, check out my EBook called 51 Recipes with jQuery and ASP.NET Controls.
Create an ASP.NET Website. Add the following markup
The layout should look similar to the following:
Now add a reference to the latest jQuery script file and write the following code:
Let us see what we just did:
When a checkbox is clicked, a variable ‘checked’ keeps track of the checked checkboxes.
We then iterate the ‘checked’ collection using $().each() and add the text of the checkbox to a variable ‘total’.
Observe how we are accessing the value of a checkbox using $(this).next().text());
This is because the checkbox gets rendered like this:
To access the value, which is inside the label, we use next() which finds the very next sibling of each checkbox, which in our case is the label control. The parseFloat( ) parses strings into numbers.
The result is displayed in a paragraph (tot) using the toFixed() method, which rounds the result to the specified number of decimal places, in our case 2.
$('#tot').text("Your Total Amount Is: " + total.toFixed(2));
The output is shown here:
See a Live Demo. This demo has been tested in the following browsers: IE 7, IE 8, Firefox 3, Chrome 2, Safari 4.
The entire source code of this article can be downloaded over here
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!
Was this article worth reading? Share it with fellow developers too. Thanks!
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry
, DNC Magazine for Developers
. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls
and The Absolutely Awesome jQuery CookBook
Suprotim has received the prestigious Microsoft MVP award for Fifteen consecutive years. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.
Get in touch with him on Twitter @suprotimagarwal or at LinkedIn