Agnes Tom
Agnes Tom

Reputation: 356

Pushing calculate sum to input

I want to push calculate sum those after Line total: display in input not in text.

I try add some input for example:

<input id="totalline" name="totalline" value="" />

and change this:

var calculate = function(el) {
var percent = el.find('input[name="percent[]"]').val();
var additional = el.find('input[name="additional[]"]').val();
var total = el.find('span.total');
var totalValue = ($("#weight").val() * percent / (100 - additional)).toFixed(2);
total.text(totalValue);
}

to this:

var calculate = function(el) {
var percent = el.find('input[name="percent[]"]').val();
var additional = el.find('input[name="additional[]"]').val();
var total = el.find('span.total');
var totalValue = ($("#weight").val() * percent / (100 - additional)).toFixed(2);
$('#totalline').val(totalValue); //<-this line changed
}

But this is not work like I want to.

Here is my fiddle.

Upvotes: 1

Views: 86

Answers (1)

mhodges
mhodges

Reputation: 11116

You are trying to use an ID selector when there will be multiple elements with the same ID on the page (when you press the + button to add a new row). Simply change your selector to var total = el.find("[name='totalline']"); to ensure that you are always grabbing the correct input.

This is what it should look like:

HTML

Line total: <input name="totalline" value="" />

JS

var calculate = function(el) {
  var percent = el.find('input[name="percent[]"]').val();
  var additional = el.find('input[name="additional[]"]').val();
  var total = el.find("[name='totalline']");
  var totalValue = ($("#weight").val() * percent / (100 - additional)).toFixed(2);
  total.val(totalValue);
}

Upvotes: 3

Related Questions