Sum of all TextBox values using jQuery




Here’s a simple script that sums up all textbox values using jQuery. Please note that I am not using any validation here to validate the data entered. The script simply shows how to sum up textbox values.

<html xmlns="http://www.w3.org/1999/xhtml">
<
head>
<
title>Sum of all TextBox values using jQuery</title>
<
script type="text/javascript"
src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.2.min.js">
</
script>
<
script type="text/javascript">
$(function() {
$("#addAll").click(function() {
var add = 0;
$(".amt").each(function() {
add += Number($(this).val());
});
$("#para").text("Sum of all textboxes is : " + add);
});
});
</script>
</
head>
<
body>
<
input id="Text1" class="amt" type="text" /><br />
<
input id="Text2" class="amt" type="text" /><br />
<
input id="Text3" class="amt" type="text" /><br />
<
input id="addAll" type="button" value="Sum all Textboxes" /><br />
<
p id="para" />
</
body>
</
html>

Live Demo

Sum TextBoxes jQuery



Will you give this article a +1 ? Thanks in advance




About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

2 Responses to "Sum of all TextBox values using jQuery"
  1. Carlo Bertini [WaYdotNET] said...
    April 30, 2010 at 8:36 AM

    //.toFixed() method will roundoff the final sum to 2 decimal places
    $("#para").text("Sum of all textboxes is : " + add.toFixed(2));

  2. Hamish Mann said...
    April 15, 2013 at 1:21 AM

    jQuery is a pretty strong lib. It provides a very awesome web browser abstraction along with a convenient syntax.

 

Copyright © 2009-2014 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions