How to auto calculate the sum of input values using Jquery or Javascript


How to auto calculate the sum of input values using Jquery or Javascript

In this tutorial, we will learn to auto calculate the sum of input values using jQuery or JavaScript. Sometimes we need addition, subtraction, division, multiplication, and percentage, etc. in a webpage on the fly i.e. without reloading the page or submitting the page. With the help of JavaScript events, we can find the values.

In this example, three input fields are taken as shown in the image above. We are finding the amount of some values based on the given percentage.

Required Steps to get the sum of input values using Jquery

  • Declare all the input fields with the id attributes of each input field to find the desired values in each row.
  • Also declare the input fields with the class attributes of each input field to collect the sum of all the desired fields. Every time we do calculations, the class attribute will help us to find the sum of all inputs.
  • We use a JavaScript event called blur as a function. All calculations including the sum of all the input fields are defined inside this function.

Also read, add rows to table dynamically using JavaScript

Complete Code:-

<html>
<head>
<title>ajax example</title>
<link rel="stylesheet" href="bootstrap.css" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="bootstrap-theme.css" crossorigin="anonymous">
<style>
.container{
	width:50%;
	height:30%;
	padding:20px;
}
</style>
</head>
<body>
	<div class="container-fluid">
		<h3 align="center">Calculate the sum of input values using Jquery or Javascript</h3>
		<br/><br/><br/>
<form class="form-horizontal" action="#">
	<div id="newuser"></div>
		  <div class="form-group row">
		        <label class="control-label col-sm-2" for="value">Value:</label>
			    <div class="col-sm-2">
			      <input type="text" name="value" id="value" class="value">
			    </div>
			    <label class="control-label col-sm-1" for="Percentage">Percentage:</label>
			    <div class="col-sm-2">
				     <input type="text" name="rate" id="rate" class="rate" onblur="getAmount()">
				</div>
				<label class="control-label col-sm-1" for="Amount">Amount:</label>
			    <div class="col-sm-2">
			      <input type="text" name="amount" id="amount" class="amount">
			    </div>
			    
		  </div>
		  <div class="form-group row">
		        <label class="control-label col-sm-2" for="Value">Value:</label>
			    <div class="col-sm-2">
			      <input type="text" name="value" id="value2" class="value">
			    </div>
			    <label class="control-label col-sm-1" for="Percentage">Percentage:</label>
			    <div class="col-sm-2">
				     <input type="text" name="rate" id="rate2" class="rate" onblur="getAmount()">
				</div>
				<label class="control-label col-sm-1" for="Amount">Amount:</label>
			    <div class="col-sm-2">
			      <input type="text" name="amount" id="amount2" class="amount">
			    </div>
			    
		  </div>
		<br/><br>
		<p style="text-align: center;font-weight: bold;">
      Total Value:<input type="text" style="border: none;border-bottom: 1px solid #756D6D;outline: none;" id="total_value"><br>
      Total Percent:<input type="text" style="border: none;border-bottom: 1px solid #756D6D;outline: none;" id="total_rate"><br>
      Total Amount:<input type="text" style="border: none;border-bottom: 1px solid #756D6D;outline: none;" id="total_amount"><br>
       </p>
		  
</form>
</div>
<script src="jquery-3.2.1.min.js"></script>
<script src="bootstrap.min.js"></script>
<script>
  function getAmount(){
  	var value = $('#value').val();
  	var percent = $('#rate').val();
  	$('#amount').val(value*percent/100);

  	var value2 = $('#value2').val();
  	var percent2 = $('#rate2').val();
  	$('#amount2').val(value2*percent2/100);

  	 //get the sum of each column of each row
  var sum_value = 0;
  $('.value').each(function(){
  	sum_value += +$(this).val();
  	$('#total_value').val(sum_value);
  })

  var sum_rate = 0;
  $('.rate').each(function(){
  	sum_rate += +$(this).val();
    $('#total_rate').val(sum_rate);
  })

  var sum_amount = 0;
  $('.amount').each(function(){
  	sum_amount += +$(this).val();
    $('#total_amount').val(sum_amount);
  })

 
  }
</script>
</body>
</html>

NOTE*

Download the bootstrap CSS and js files from google and include the path of the files in the href attribute of link tag and src attribute of the script tag respectively.

Also read, How To Export MySQL Table Data Into Excel Using jQuery

To know more about the JavaScript auto calculation, visit here

CONCLUSION:- I hope this article will help you to auto calculate the sum of input fields using Jquery or Javascript easily. If you have any doubt then please leave your comment below.


1 thought on “How to auto calculate the sum of input values using Jquery or Javascript”

  1. I have to thank you for the efforts you have put in penning this
    website. I am hoping to view the same high-grade blog posts from you in the
    future as well. In fact, your creative writing abilities has inspired me to get my very own site now 😉

    Reply

Leave a Comment

CERI188

THOR311 RESMI

THOR311 AKSES

THOR311

THOR311 RTP

RATU311

RATU311

THOR311

parlay

CERI188

HOKI311

RATU311

RATU311

judi bola online

Mix Parlay

THOR311

Ceri188

Thor311 Game Online

Thor311 Aplikasi

LINK CERI188

RATU311 NONTON BOLA

RATU311

HOKI311 Link Alternatif

CERI188 Link RTP LIVE

THOR311

THOR311

Thor311 Live Casino

CERI188

akses VIP THOR311

THOR311

link daftar THOR311

THOR311

situs thor311

THOR311 LOGIN

THOR311

THOR311

THOR311

RATU311

RATU311

HOKI311 Link SABUNG AYAM RESMI

HOKI311 Link QRIS EWALLET

RATU311

LINK CERI188

parlay

HOKI311 Link RTP Gacor

THOR311

RATU311

THOR311

THOR311

HOKI311 Link Apk Resmi

RATU311

KUPU178

RATU311

RATU311

RATU311

CERI188

CERI188 Situs Spaceman Terbaik 2026

KUPU178 Link Daftar

CERI188

CERI188

CERI188

CERI188

CERI188

HOKI311

THOR311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

CERI188 Live Casino

HOKI311 Lapak Arena Slot

RATU311 judi bola

CERI188 Live Casino

HOKI311 Link Spaceman Online

RATU311

HOKI311 Link Daftar Resmi

CERI188 Toto SGP Online

KUPU178

KUPU178

KUPU178

KUPU178

HOKI311 TEMBAK IKAN ONLINE

RATU311

CERI188 PANDUAN MAXWIN SLOT ONLINE

RATU311

RATU311

RATU311

KUPU178

HOKI311 Link Login Alternatif

HOKI311 Live Casino Online

HOKI311 Situs Slot Gacor

THOR311

THOR311

situs judi bola

THOR311

Ceri188 Game Online

CERI188

link login CERI188

THOR311

link login CERI188

CERI188

link login CERI188

link login CERI188

link daftar CERI188

THOR311

HOKI311

HOKI311

CERI188

CERI188

CERI188

parlay

THOR311

RATU311

RATU311

HOKI311

HOKI311

parlay

HOKI311

RATU311

THOR311

KUPU178

HOKI311

KUPU178

KUPU178

KUPU178

HOKI311

parlay

slot gacor

kupu178 link alternatif

kupu178 slot

kupu178 daftar

kupu178 login

link daftar kupu178

judi bola

HOKI311

RATU311

link alternatif RATU311

Link daftar RATU311

link login RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

KUPU178

KUPU178

THOR311

KUPU178

KUPU178

KUPU178

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

THOR311

RATU311

KUPU178

KUPU178

HOKI311

THOR311

KUPU178

THOR311

THOR311

slot thailand

judi bola

THOR311

THOR311

KUPU178

THOR311

situs bola

THOR311

slot gacor

slot gacor

yakuza303

slot gacor

unobet77

slot gacor

slot gacor

RATU311

parlay

parlay

slot gacor

mix parlay

judi bola

judi bola

mix parlay

parlay

judi bola

sv388

judi bola

judi bola

judi bola

parlay

THOR311