Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to calculate 3 textbox multiply values in real time?

Posted on 2009-04-05
4
Medium Priority
?
1,371 Views
Last Modified: 2013-12-17
I have aspx page which has 4 textbox.

txtAmount, txtExchangeRate, txtCharges, txtResult

When you input txtAmount, txtExchangeRate, txtCharges, then txtResult will show the calculated result.

e.g. 405 * 0.88 + 20 = result

As there are many rows in that listInput, how can it be done to show the result in real time?
0
Comment
Question by:techques
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24074769
With straight JS I would do this - I hope this is NOT homework - it is it, next time please start the html and script yourself and ask if you are stuck:


<html>
  <head>
  <title></title>
<script type="text/javascript">
function calc(theForm) {
  var amount = parseFloat(theForm.txtAmount.value)
  if (isNaN(amount)) amount=0;
  var exchangeRate = parseFloat(theForm.txtExchangeRate.value)
  if (isNaN(exchangeRate)) exchangeRate=0;
  var charges = parseFloat(theForm.txtCharges.value)
  if (isNaN(charges)) charges=0;
  var result =  (amount * exchangeRate) + charges
  theForm.txtResults.value=result.toFixed(2);
}
</script>
  </head>
  <body>
<form>
<input type="text" name="txtAmount"       value="" onKeyUp="calc(this.form)" /> *
<input type="text" name="txtExchangeRate" value="" onKeyUp="calc(this.form)" /> +
<input type="text" name="txtCharges"      value="" onKeyUp="calc(this.form)" /> =
<input type="text" name="txtResults"      value="0.00" readonly="readonly" />
</form>
  </body>
</html>

Open in new window

0
 
LVL 6

Assisted Solution

by:havj123
havj123 earned 60 total points
ID: 24074814
Use this

<table>
        <tr>
            <td>
                <input type="text" id="txtAmount" name="txt1" />
                <input type="text" id="txtExchangeRate" name="txt2"/>
                <input type="text" id="txtCharges" onchange="calculate();" name="txt3" />
                <input type="text" id="txtResult" name="txt4"/>
            </td>
        </tr>
    </table>

javascript function:-

<script type="text/javascript">
        function calculate()
        {
            var amt, ex, ch, result;
            amt = document.getElementById("txtAmount").value;
            ex = document.getElementById("txtExchangeRate").value;
            ch = document.getElementById("txtCharges").value;
            result = document.getElementById("txtResult");
           
            if(amt != "" || ex != "" || ch != "" || amt != null || ex != null || ch != null)
            {
                result.value = parseInt(amt*ex)+parseInt(ch);
            }
        }
 </script>

Note: either make txtAmount, txtExchangeRate, txtCharges required or convert empty string to 0.
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 90 total points
ID: 24074845
Alternatively if you need to calculate rows of data, try this:

<html>
  <head>
  <title></title>
<script type="text/javascript">
function calc(theField) {
  var id = theField.name.split('_')[1]; // change this to find the common part of the names
  var theForm = theField.form;
  var amount = parseFloat(theForm.elements["txtAmount_"+id].value)
  if (isNaN(amount)) amount=0;
  var exchangeRate = parseFloat(theForm.elements["txtExchangeRate_"+id].value)
  if (isNaN(exchangeRate)) exchangeRate=0;
  var charges = parseFloat(theForm.elements["txtCharges_"+id].value)
  if (isNaN(charges)) charges=0;
  var result =  (amount * exchangeRate) + charges
  theForm.elements["txtResults_"+id].value=result.toFixed(2);
}
</script>
  </head>
  <body>
    <form>
      <input type="text" name="txtAmount_1"       value="" onKeyUp="calc(this)" /> *
      <input type="text" name="txtExchangeRate_1" value="" onKeyUp="calc(this)" /> +
      <input type="text" name="txtCharges_1"      value="" onKeyUp="calc(this)" /> =
      <input type="text" name="txtResults_1"      value="0.00" readonly="readonly" /><br />
      <input type="text" name="txtAmount_2"       value="" onKeyUp="calc(this)" /> *
      <input type="text" name="txtExchangeRate_2" value="" onKeyUp="calc(this)" /> +
      <input type="text" name="txtCharges_2"      value="" onKeyUp="calc(this)" /> =
      <input type="text" name="txtResults_2"      value="0.00" readonly="readonly" /><br />
    </form>
  </body>
</html>

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24076604
erm 50 points, split and then an uncommented "B" grade???
0

Featured Post

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question