Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
HTML
<html>
<body>
<form>
  Addition 1:<br>
  <input type="text" name="add1">
  <br>
  Subtraction 1:<br>
  <input type="text" name="sub1">
<br>

 Addition 2:<br>
  <input type="text" name="add2">
  <br>
  Subtraction 2:<br>
  <input type="text" name="sub2"><br> <br> 
<label>Total</label>
<input type="text" name="total"  disabled>
</form>

</body>
</html>
Posted
Updated 14-Dec-15 21:43pm
v2
Comments
Suvendu Shekhar Giri 15-Dec-15 3:29am    
Where is the code ?
Abim usman 15-Dec-15 6:17am    
Sorry my post wrong. not subtraction means multiplication.

My code Below. The addition functions properly, the multiplication wont work directly to the over all total.

I would like to fix this problem.

<html>
<body>

<table>
<tr>

Addition and Multiplication


</tr>

<tr>
<td>Lg Tv</td>
<td><input type='text' class='Multiplication' placeholder="Quantity "/></td>

</tr>
<tr>
<td>Price</td>
<td><input type='text' class='price' placeholder="Price "/></td>
</tr>
<tr>
<td>Samsung Tv</td>
<td><input type='text' class='Multiplication' placeholder="Quantity "/></td>
</tr>




<tr>
<td>Price</td>
<td><input type='text' class='price' placeholder="Price "/></td>
</tr>
<tr>
<td>Over all Total</td>
<td><input type='text' id='totalPrice' disabled /></td>
</tr>
</table>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js "></script>

<script>
// we used jQuery 'keyup' to trigger the computation as the user type
$('.price').keyup(function () {

// initialize the sum (total price) to zero
var sum = 0;

// we use jQuery each() to loop through all the textbox with 'price' class
// and compute the sum for each loop
$('.price').each(function() {
sum += Number($(this).val());
});

// set the computed value to 'totalPrice' textbox
$('#totalPrice').val(sum);

});
</script>

</body>
</html>
OriginalGriff 15-Dec-15 3:32am    
And?
What have you tried?
Where are you stuck?
What help do you need?
Abim usman 15-Dec-15 6:23am    
Sorry my post wrong. not subtraction is multiplication.

My code Below. The addition functions properly, the multiplication wont work directly to the over all total.

I would like to fix this problem.

<html>
<body>

<table>
<tr>

Addition and Multiplication



</tr>

<tr>
<td>Lg Tv</td>
<td><input type='text' class='Multiplication' placeholder="Quantity "/></td>

</tr>
<tr>
<td>Price</td>
<td><input type='text' class='price' placeholder="Price "/></td>
</tr>
<tr>
<td>Samsung Tv</td>
<td><input type='text' class='Multiplication' placeholder="Quantity "/></td>
</tr>




<tr>
<td>Price</td>
<td><input type='text' class='price' placeholder="Price "/></td>
</tr>
<tr>
<td>Over all Total</td>
<td><input type='text' id='totalPrice' disabled /></td>
</tr>
</table>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js "></script>

<script>
// we used jQuery 'keyup' to trigger the computation as the user type
$('.price').keyup(function () {

// initialize the sum (total price) to zero
var sum = 0;

// we use jQuery each() to loop through all the textbox with 'price' class
// and compute the sum for each loop
$('.price').each(function() {
sum += Number($(this).val());
});

// set the computed value to 'totalPrice' textbox
$('#totalPrice').val(sum);

});
</script>

</body>
</html>
Abim usman 15-Dec-15 6:26am    
Sorry my post is wrong. not subtraction means multiplication

My code Below. The addition functions properly, the multiplication wont work directly to the over all total.

I would like to fix this problem.

<html>
<body>

<table>
<tr>

Addition and Multiplication



</tr>

<tr>
<td>Lg Tv</td>
<td><input type='text' class='Multiplication' placeholder="Quantity "/></td>

</tr>
<tr>
<td>Price</td>
<td><input type='text' class='price' placeholder="Price "/></td>
</tr>
<tr>
<td>Samsung Tv</td>
<td><input type='text' class='Multiplication' placeholder="Quantity "/></td>
</tr>




<tr>
<td>Price</td>
<td><input type='text' class='price' placeholder="Price "/></td>
</tr>
<tr>
<td>Over all Total</td>
<td><input type='text' id='totalPrice' disabled /></td>
</tr>
</table>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js "></script>

<script>
// we used jQuery 'keyup' to trigger the computation as the user type
$('.price').keyup(function () {

// initialize the sum (total price) to zero
var sum = 0;

// we use jQuery each() to loop through all the textbox with 'price' class
// and compute the sum for each loop
$('.price').each(function() {
sum += Number($(this).val());
});

// set the computed value to 'totalPrice' textbox
$('#totalPrice').val(sum);

});
</script>

</body>
</html>

1 solution

Here is an example :
Html code :
HTML
<div>
        Addition 1:<br />
        <input type="text" id="add1" name="add1" onkeyup="AutoCalc(this)" />
        <br />
        Subtraction 1:<br />
        <input type="text" id="sub1" name="sub1" onkeyup="AutoCalc(this)" />
        <br />
        Addition 2:<br />
        <input type="text" id="add2"  name="add2" onkeyup="AutoCalc(this)" />
       <br />
        Subtraction 2:<br />
        <input type="text" id="sub2" name="sub2" onkeyup="AutoCalc(this)" />
        <br />
        <br />
        <label>
            Total</label>
        <input type="text" id="total" name="total" disabled="disabled" />
    </div>


And Javascript :
JavaScript
function AutoCalc(obj) {
           var total = 0;

           if (isNaN(obj.value)) {
               alert("Please enter a number :(");
               obj.value = '';
               return false;
           }
           else {

               var textBox = new Array();
               textBox = document.getElementsByTagName('input')

               for (i = 0; i < textBox.length; i++) {
                   if (textBox[i].type == 'text') {
                       var inputVal = textBox[i].value;
                       if (inputVal == '')
                           inputVal = 0;
                       if ((textBox[i].id == 'add1') || (textBox[i].id == 'add2')) {
                           total = total + parseInt(inputVal);
                       }
                       if ((textBox[i].id == 'sub1') || (textBox[i].id == 'sub2')) {
                           total = total - parseInt(inputVal);
                       }
                   }
               }
               document.getElementById('total').value = total;
           }
       }


Hope it helps you.
 
Share this answer
 
Comments
Abim usman 15-Dec-15 6:36am    
Your code is right, but sorry my post is wrong. is not subtraction but multiplication. im beginner can you change your code to multiplication.
Raje_ 15-Dec-15 6:43am    
Where do you want to multiply? Can you please update your question?
Abim usman 15-Dec-15 6:56am    
subtraction change to multiplication

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900