Javascript help

Explorer ,
Feb 24, 2021 Feb 24, 2021

Copy link to clipboard

Copied

I have a code and it's sort of working, here is my problem, I'm calculating totals with a loop and I want to show result in first field that is empty, I managed to make this work but it shows result in all empty fields not just first one that is empty and it's doubling result.

here is example file:

https://drive.google.com/uc?export=download&id=1g7UKiR3Zln0ngn5czrXCfdLtDgliZop6 

Code is in "t10" field.

TOPICS
JavaScript

Views

60

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct Answer

Adobe Community Professional , Feb 24, 2021 Feb 24, 2021
Try this: var tot = 0; var to2 = 0; for (var i=1; i<=10; i++) { if(this.getField("t"+i).value != "")tot++; var f = tot+1; if (this.getField("Text"+i).value!= "" && this.getField("p"+i).value != "") { this.getField("t"+i).value = Number(this.getField("Text"+i).value) * Number(this.getField("p"+i).value); to2 += Number(this.getField("t"+i).value); this.getField("t"+f).value = to2; } } Info: The field with the name p7 doesn't exists.

Likes

Translate

Translate
Adobe Community Professional ,
Feb 24, 2021 Feb 24, 2021

Copy link to clipboard

Copied

There are errors in your code. Check the JS Console.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 24, 2021 Feb 24, 2021

Copy link to clipboard

Copied

Yes it says: this.getField(...) is null 11:Console:Exec I don't know what that mean, Although the code is working I just want it to populate only first empty field with sum total and not all of them.

Can you point me in right direction?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Feb 24, 2021 Feb 24, 2021

Copy link to clipboard

Copied

Try this:

var tot = 0;
var to2 = 0;
for (var i=1; i<=10; i++) {
  if(this.getField("t"+i).value != "")tot++;
  var f = tot+1;
  if (this.getField("Text"+i).value!= "" && this.getField("p"+i).value != "") {
    this.getField("t"+i).value = Number(this.getField("Text"+i).value) * 
    Number(this.getField("p"+i).value);

    to2 += Number(this.getField("t"+i).value);

    this.getField("t"+f).value = to2;
  }
}

Info: The field with the name p7 doesn't exists.

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 24, 2021 Feb 24, 2021

Copy link to clipboard

Copied

LATEST

Wait, what it was just those curly brackets 🙂

Thank you Bernd it's working now .

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines