user3108268
user3108268

Reputation: 1083

Copy value to another input with a button

I have two input number fields that show total with some jQuery.

Calculate total:

$('form#lines-form-1 :input[type="number"]').change(function() {
  var tot = 0;
  $('form#lines-form-1 :input[type="number"]').each(function() {
    tot += +this.value;
  });
  $('#tot-qty').text(tot);
});

JSFiddle: https://jsfiddle.net/q3z4w98o/2/

After entering some numbers on form A press Total to show results.

When clicked Copy how to transfer the total value to Form B's first input field?

E.g.

  1. input1 has value 1
  2. input2 has value 1
  3. total = 2
  4. click Copy
  5. input3 gets a value of 2

enter image description here

Upvotes: 0

Views: 814

Answers (1)

Sunny Patel
Sunny Patel

Reputation: 8078

Use the following code JS Fiddle:

$("button.copy-btn").click(function() {
    $('input[name="i3"]').val($("#tot-qty").text());
})

This bit copies the text value inside the #tot-qty span and sets the i3 input value to that when clicking the "Copy" button.

Upvotes: 2

Related Questions