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

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

361

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

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.

 

Votes

Translate

Translate
Community Expert ,
Feb 24, 2021 Feb 24, 2021

Copy link to clipboard

Copied

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

Votes

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?

Votes

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

 

Votes

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 .

Votes

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