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

Javascript Loop to Assign Values to Fields

New Here ,
Nov 09, 2023 Nov 09, 2023

Copy link to clipboard

Copied

I have 4 fields that are named prc10, prc20, prc30, and prc40, along with a couple of ther fields. I have a button that when pressed is supposed to loop through all the fields in the form, and if the field's name starts with "prc" it assigns the number after the "prc" as the value to the field.

 

Here's the code I have assigned to the button:

 

for(i=0;i<numFields;i++){
    var strname = getField(getNthFieldName(i)).name;
    var prcField = this.getField(strname);
    
    if (strname.startsWith("prc")) {
       prcField.value = strname.substring(3);   
    }
}

When I press the button, it assigns the value of the first prc field to 10, but leaves the other 3 empty. If I press the button again it assigns the value of 20 to the second field and leaves the next two empty. So I need to press the button 4 times to get it to assign values to the 4 fields.

 

I commented out the line where the value is getting assigned to the field and outputting strname.substring(3) to the console to make sure that's working and after clicking the button once, it outputs 10, 20, 30, 40.

 

Any ideas why it doesn't assign all the fields with one click?

TOPICS
Acrobat SDK and JavaScript

Views

289

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 , Nov 09, 2023 Nov 09, 2023

It should assign all values with one click.

Where did you put the script?

Try to recreate fields in new file and test.

EDIT:

You can also try to replace startsWith with indexOf() and see if that will work, somethin like this:

if (strname.indexOf("prc") === 0) {

 

Votes

Translate

Translate
Community Expert ,
Nov 09, 2023 Nov 09, 2023

Copy link to clipboard

Copied

It should assign all values with one click.

Where did you put the script?

Try to recreate fields in new file and test.

EDIT:

You can also try to replace startsWith with indexOf() and see if that will work, somethin like this:

if (strname.indexOf("prc") === 0) {

 

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
New Here ,
Nov 09, 2023 Nov 09, 2023

Copy link to clipboard

Copied

Thanks, that fixed everything!

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 ,
Nov 09, 2023 Nov 09, 2023

Copy link to clipboard

Copied

You're code is a bit redundant:

var strname = getField(getNthFieldName(i)).name;

is the same as 

var strname = this.getNthFieldName(i);

 

The variables also don't need to be redeclared everytime through the loop. Don't know if either if these issues has to do with the problem, but it's always good to have good form. 

Also, if you already know the names of the fields, why loop through all of them, instead of only looping through the ones that count. 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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
New Here ,
Nov 09, 2023 Nov 09, 2023

Copy link to clipboard

Copied

Realized that after I posted it. Here's what I've landed on:

for(var i=0;i<this.numFields;i++){
    prcField = getField(getNthFieldName(i));
    strname = prcField.name;

    if (strname.indexOf("prc") === 0) {
        prcField.value = strname.substring(3);  
    }
}

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
New Here ,
Nov 09, 2023 Nov 09, 2023

Copy link to clipboard

Copied

LATEST

Also, the final document is going to have hundreds of fields so I'm trying to make it more flexible. 

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