Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Javascript help

Explorer ,
Feb 24, 2021 Feb 24, 2021

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
640
Translate
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
1 ACCEPTED SOLUTION
Community Expert ,
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.

 

View solution in original post

Translate
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
Community Expert ,
Feb 24, 2021 Feb 24, 2021

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

Translate
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

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?

Translate
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
Community Expert ,
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.

 

Translate
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
LATEST

Wait, what it was just those curly brackets 🙂

Thank you Bernd it's working now .

Translate
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