0

How to multiplication array input and display result. If input order, the price will multiplication with order and display subtotal. The problem is I have more than one subtotal

<table>
<tr>
    <td>Price</td>
    <td>Order</td>
    <td>Subtotal</td>
</tr>
<tr>
    <td><input type="text" id="price1" name="price" readonly="" value="15000"></td>
    <td><input type="text" id="order1" name="order" value="0" oninput="subt()"></td>
    <td><input type="text" name="subtotal" value="0" readonly></td>
</tr>
<tr>
    <td><input type="text" id="price2" name="price" readonly="" value="10000"></td>
    <td><input type="text" id="order2" name="order" value="0" oninput="subt()"></td>
    <td><input type="text" name="subtotal" value="0" readonly></td>
</tr>
<tr>
    <td><input type="text" id="price3" name="price" readonly="" value="5000"></td>
    <td><input type="text" id="order3" name="order" value="0" oninput="subt()"></td>
    <td><input type="text" name="subtotal" value="0" readonly></td>
</tr>

Javascript

function subt(){

var price = document.getElementsByName('price'),
    order = document.getElementsByName('order'),
    subtotal = document.getElementsByName('subtotal');

var sub=0;

for(var i=0;i<price.length;i++){
    sub += price[i] * order[i];
    document.getElementByName('subtotal').value = sub; 
    }

}
3
  • You question is not clear Commented Oct 26, 2017 at 3:43
  • I want to find subtotal for every input, where subtotal is price * order. I have code to find subtotal for one result, but now I have 3 input and 3 subtotal Commented Oct 26, 2017 at 3:49
  • got it, please take a look at my answer Commented Oct 26, 2017 at 3:54

2 Answers 2

1

In the subt function pass the current input field by passing this which refers to the current element in context. Along with that pass the value of the price and the name of the input element where you want to display the subtotal.

Also getElementsByName is a collection. In order to get the field , you need to pass the index. Here [0] mean element in first index

function subt(elem, val, dispField) {
  document.getElementsByName(dispField)[0].value = elem.value * val
}
<table>
  <tr>
    <td>Price</td>
    <td>Order</td>
    <td>Subtotal</td>
  </tr>
  <tr>
    <td><input type="text" id="price1" name="price" readonly="" value="15000"></td>
    <td><input type="text" id="order1" name="order" value="0" oninput="subt(this,15000,'subtotal1')"></td>
    <td><input type="text" name="subtotal1" value="0" readonly></td>
  </tr>
  <tr>
    <td><input type="text" id="price2" name="price" readonly="" value="10000"></td>
    <td><input type="text" id="order2" name="order" value="0" oninput="subt(this,10000,'subtotal2')"></td>
    <td><input type="text" name="subtotal2" value="0" readonly></td>
  </tr>
  <tr>
    <td><input type="text" id="price3" name="price" readonly="" value="5000"></td>
    <td><input type="text" id="order3" name="order" value="0" oninput="subt(this,5000,'subtotal3')"></td>
    <td><input type="text" name="subtotal3" value="0" readonly></td>
  </tr>
</table>

Sign up to request clarification or add additional context in comments.

Comments

1

I recommend adding an ID to each subtotal and refactoring your code as follows- either:

  1. Looping through rows and targeting inputs by ID, eliminating array variables.

    • Detect the number of table rows
    • set your for loop to iterate through each row
    • in the loop, set each element ID by concatenation the string with the integer from the loop counter
    • get element by ID instead of name to parse input parameters
    • perform calculation
    • get subtotal element by ID and set value
  2. If table will always have fixed number of columns, iterate similar to above but target inputs by the column position, using nth child identifiers. This can eliminate any id or class targeting.

Worth noting, for valid html, you should add id's and names to each input field, and it's recommended to include labels for accessibility

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.