How to Auto Calculate form field in Javascript?

Today, We want to share with you javascript calculation form field values.In this post we will show you Real time calculation with custom fields, hear for calculate two input field values in javascript we will give you demo and example for implement.In this post, we will learn about Javascript Onchange Calculate Total Quantity Price Demo with an example.

How to Auto Calculate Price in Javascript?

Example 1: index.html

<!DOCTYPE html>
<html>
<body>
    <h2>How to Auto Calculate Price in Javascript - www.pakainfo.com</h2>
    <form name="frm-pin" method="post" action="pin-index-a.php">
        <input type="hidden" name="mode" value="PinRequest" />
        <label class="w3-text-green"><b>Number Of Items</b></label>
        <select name="tot_pin_requested" onchange="calcular(this.value)" required>
			<option value="" disabled selected>Select your option</option>
			<option value="1">Mobile</option>
			<option value="2">Pendrive</option>
			<option value="3">LCD</option>
			<option value="4">Computer</option>
			<option value="5">Laptop</option>
			<option value="6">Car</option>
			<option value="7">Bike</option>
			<option value="8">Iphone</option>
			<option value="9">Home</option>
			<option value="10">Khatri</option>
		</select>
        <label><b>Total Amount</b></label>
        <input class="w3-input w3-border" name="item_price" id="item_price" type="text" readonly>
        <script>
            function calcular(val) {
                var tot_price = val * 100;
                /*prints the result*/
                var results = document.getElementById('item_price');
                results.value = tot_price;
            }
        </script>
        <body>
</html>

Sum of 2 inputs and appear in real time

Example 2:

<form action="" method="post">
  First Number: <input type="text" id="n1" value="10" /> <br> Second Number: <input type="text" id="n2" value="5" onblur="calcular()" /> <br>
</form>

<div id="resultado"></div>

javaScript Code

function calcular() {
  var n1 = parseInt(document.getElementById('n1').value, 10);
  var n2 = parseInt(document.getElementById('n2').value, 10);
  document.getElementById('resultado').innerHTML = n1 + n2;
}

I hope you get an idea about javascript onchange calculate total quantity price demo.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Read Also:  How to Generate JavaScript Random Numbers?

About Pakainfo

I am Jaydeep Gondaliya, a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Pakainfo →

Leave a Reply

Your email address will not be published. Required fields are marked *