Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calculate two input field values in javascript

Hi i want to calculate two input field values and result will show in third input field so i want to write code in ajax page

<input id="a1" type="text" />
<input id="a2" type="text" onblur="Calculate();"  />
<input id="a3" type="text" name="total_amt" value="" />

here javascript function

 <script>


function Calculate()
{
var resources = document.getElementById('a1').value;
var minutes = document.getElementById('a2').value; 
document.getElementById('a3').value=parseInt(resources) *       parseInt(minutes);
 document.form1.submit();
   }
 </script>

starting its working but nw its not working please help me

Thanks in Advance


2 Answers

Look this! Work it. http://jsfiddle.net/op1u4ht7/2/

<input id="a1" type="text" />
<input id="a2" type="text" onblur="calculate()"  />
<input id="a3" type="text" name="total_amt" />

calculate = function()
{
    var resources = document.getElementById('a1').value;
    var minutes = document.getElementById('a2').value; 
    document.getElementById('a3').value = parseInt(resources)*parseInt(minutes);

   }
like image 73
Maybe Gordon Avatar answered Sep 09 '25 04:09

Maybe Gordon


Try AutoCalculator https://github.com/JavscriptLab/autocalculate Calculate Inputs value and Output By using selector expressions

Just add an attribute for your output input like data-ac="(#firstinput+#secondinput)"

No Need of any initialization just add data-ac attribute only. It will find out dynamically added elements automatically

FOr add 'Rs' with Output just add inside curly bracket data-ac="{Rs}(#firstinput+#secondinput)"

like image 42
Justine Jose Avatar answered Sep 09 '25 02:09

Justine Jose