1

I am trying to sum numbers from 2 text box and the result to a 3rd text box in a PDF Form (Property of text Boxes set to multiline)

Tried the following code but it is not working to add the values and display the results in each row.

var values1 = this.getField("Text6").value.split("\n");
var values2 = this.getField("Text7").value.split("\n");

this.getField("Text8").value = (values1 + values2 );

Unfortunately this is not working.Can someone help me on this?

Expected output below enter image description here

1
  • Please provide more complete code, like a minimally working example using the snippet functionality so we can more easily help you. Commented May 12, 2022 at 21:27

1 Answer 1

1

You're split()ing a string into arrays, you can't add arrays. You'll need to loop through the arrays to add each number.

Also, the array is an array of strings, because you got it from the value of a textarea, which is going to always return a string. You need to parseInt() the string in order to turn it into a number so it will add the numbers rather than concatenate a string.

function getField(id) {
  return document.getElementById(id);
}

var values1 = this.getField("Text6").value.split("\n");
var values2 = this.getField("Text7").value.split("\n");


for(i = 0; i < values1.length; i++) {
  this.getField("txtSums").value += (parseInt(values1[i]) + parseInt(values2[i])) + "\n";
}
<textarea id="Text6">1
2
3</textarea>
<br />
<br />
<textarea id="Text7">4
5
6</textarea>

<br /><br />

sums:<br />
<textarea id="txtSums"></textarea>

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

9 Comments

I am just wondering instead of Grand total (21) how to achieve the total for each rows?
Is your code in Java or JavaScript? That looks like Java... I gave you the answer in JavaScript as your question is tagged with JavaScript.
I am using JavaScript in PDF Form
I've updated my answer to show you how to have two sums from each textarea, then a sum of both of those as the total sum.
Sorry for this...Can you update the code to get the sum like 1+4 =5 ,2+5=7,3+6=9 up to the end
|

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.