Skip to main content
Participant
November 9, 2023
Answered

Javascript Loop to Assign Values to Fields

  • November 9, 2023
  • 2 replies
  • 883 views

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?

This topic has been closed for replies.
Correct answer Nesa Nurani

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) {

 

2 replies

Thom Parker
Community Expert
Community Expert
November 9, 2023

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 PDFScriptingUse the Acrobat JavaScript Reference early and often
Participant
November 10, 2023

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);  
    }
}
Participant
November 10, 2023

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

Nesa Nurani
Community Expert
Nesa NuraniCommunity ExpertCorrect answer
Community Expert
November 9, 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) {

 

Participant
November 10, 2023

Thanks, that fixed everything!