0

I have website in which I have a form, where there is a input fields like below. As you can see I'm trying to add two input boxes to get the values of the third, however this is working only with first set of input box, for the rest its coming faulty or the values of first set. How can I fix this?

$(document).ready(function() {
  $(".value2").keyup(function() {
    var val1 = +$(".value1").val();
    var val2 = +$(".value2").val();
    $(".result").val(val1 * val2);
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<input name="quantity[]" type="text" class="form-control value1">
<input name="rate[]" type="text" class="form-control value2">
<input name="amount[]" type="text" class="form-control result">

<input name="quantity[]" type="text" class="form-control value1">
<input name="rate[]" type="text" class="form-control value2">
<input name="amount[]" type="text" class="form-control result">

<input name="quantity[]" type="text" class="form-control value1">
<input name="rate[]" type="text" class="form-control value2">
<input name="amount[]" type="text" class="form-control result">

2 Answers 2

1

Wrap in containers and delegate from the parent div of the element you update

The data here is relative to the field you update

NOTE: Add a main container with an ID and wrap each set in divs too

$(document).ready(function() {
  $("#container").on("input",".form-control", function() {
    const $parent = $(this).closest("div.item")
    var val1 = +$(".value1", $parent).val();
    var val2 = +$(".value2", $parent).val();
    $(".result", $parent).val(val1 * val2);
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<div id="container">
<div class="item">
  <input name="quantity[]" type="text" class="form-control value1">
  <input name="rate[]" type="text" class="form-control value2">
  <input name="amount[]" type="text" class="form-control result">
</div>
<div class="item">
  <input name="quantity[]" type="text" class="form-control value1">
  <input name="rate[]" type="text" class="form-control value2">
  <input name="amount[]" type="text" class="form-control result">
</div>
<div class="item">
  <input name="quantity[]" type="text" class="form-control value1">
  <input name="rate[]" type="text" class="form-control value2">
  <input name="amount[]" type="text" class="form-control result">
</div>
</div>

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

7 Comments

i tried this, i am not getting any value in final box
As you can see it works here. You have likely not copied the complete solution (ID on the container, each set in separate containers)
can we make that closest div as a class?
Sure. Give it a class and do closest(".classNameOfContainer")
its not working
|
1

The issue is because when you call val(), amongst other jQuery methods, on a jQuery object holding a collection of elements, it only evaluates against the first element in that collection.

To fix your issue you can use jQuery's DOM traversal methods to retrieve the related elements in the DOM to the one which raised the event. In this case, prev() and next():

jQuery($ => {
  $(".value2").on('input', e => {
    let $val2 = $(e.target);
    let val1 = +$val2.prev(".value1").val() || 0;
    let val2 = +$val2.val() || 0;
    $val2.next(".result").val(val1 * val2);
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<input name="quantity[]" type="text" class="form-control value1">
<input name="rate[]" type="text" class="form-control value2">
<input name="amount[]" type="text" class="form-control result">

<input name="quantity[]" type="text" class="form-control value1">
<input name="rate[]" type="text" class="form-control value2">
<input name="amount[]" type="text" class="form-control result">

<input name="quantity[]" type="text" class="form-control value1">
<input name="rate[]" type="text" class="form-control value2">
<input name="amount[]" type="text" class="form-control result">

Note in the above example the use of input over keypress so that the logic also works when the user adds content to the input using the mouse. Also note the use of || 0 to coerce a NaN value to a 0.

5 Comments

i tried this but i am not getting any value in final input box
In this case I would assume that either you've not included jQuery.js in the page (as I had to add it to the example) or the HTML you provided in the question is not the same as in your local environment, as the example work fine. Could you please edit the question with the correct HTML.
sandbox.onlinephpfunctions.com/code/… this is the complete code
Are you sure that's the right code? That has no inputs with the names provided in the question, and none of them are in columns...?
yes, it has actually an add more button, when u click on add more button it will keep on adding fields

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.