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

Arrays

Explorer ,
Apr 25, 2022 Apr 25, 2022

I've got a problem with arrays. I have a button that resets my form, but it doesn't do what I need for dropdown lists. Under "Actions" I added a piece Javascript to set the dropdown list to the "initial" state I need.

 

Now this code works:

 

this.getField("Student1").value="SSS"
this.getField("Student2").value="SSS"
this.getField("Student3").value="SSS"

 

But this doesn't work

 

v= new Array("Student1","Student2","Student3");
for (i=0; i<v.length; i++)
this.getField(v[i]).value="SSS"

 

What really confuses me is that it works for two of the three entries in the array.

 

Stumped and could use help.

 

Jack

TOPICS
PDF forms
2.4K
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 ,
Apr 25, 2022 Apr 25, 2022

Check the Javascript console for errors.

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 ,
Apr 25, 2022 Apr 25, 2022

I admit that I'm not positive how to check the javascript console, but I have Acrobat set to pop up a window when there is a problem (i.e. undefined field), and nothing comes up. (See below)

 

jgostlgostlnet_0-1650945119202.pngexpand image

 

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 ,
Apr 25, 2022 Apr 25, 2022

See if this will work:

for(var i=1; i<=3; i++)
this.getField("Student"+i).value = "SSS";

 

 

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 ,
Apr 25, 2022 Apr 25, 2022

I tried it, but it didn't stand a chance. Remember that the indices in the array are 0, 1 and 2. Not 1, 2 and 3.

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 ,
Apr 26, 2022 Apr 26, 2022

Use it without array.

 

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 ,
Apr 25, 2022 Apr 25, 2022

What happens when you use:

this.resetForm (v);

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 ,
Apr 25, 2022 Apr 25, 2022

I'm afraid it did even less. It acted exactly the same way as using the default "reset form".

 

The problem here is that there appears to be no clear definition of "resetting a dropdown list". One would assume it means setting it back and the initial default values, but apparently it is not.

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 ,
Apr 25, 2022 Apr 25, 2022

Honestly, what worries me here is why the loop itself isn't working. I've used this technique in other places and if I'm doing something wrong, I've either got to fix it or go back and change it in a few other places in the form.

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 ,
Apr 25, 2022 Apr 25, 2022

OK - Some progress, finally. Here is how I am using this. From the "Actions" for the button.

 

jgostlgostlnet_0-1650954380672.pngexpand image

and it fails. but when I change this to run the Javascript before the reset form,

jgostlgostlnet_1-1650954526035.pngexpand image

it works. Of course this causes other problems, but I can handle those. It still bothers me that the loop didn't work. There is something going on here that I don't understand.

 

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 ,
Apr 26, 2022 Apr 26, 2022

You need to perform all the actions in a single JavaScript command, as you can't control the order in which these actions are performed. It might be in the order they appear, or it might not be, so the only way to make sure it goes in the order you want it to is to do it all yourself in a single script.

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 ,
Apr 26, 2022 Apr 26, 2022

That doesn't do it either. I reduced it to one script. Here is the script in its entirety. Note the alerts to confirm what is happening. 

 

global.clearform=true
v= new Array("Student1","Student2","Student3");
nf=v.length;
app.alert("Number of fields is "+nf)
for (i=0; i<nf; i++) {
app.alert("Resetting field "+i);
this.getField(v[i]).value="SSS";
}
global.clearform=false

 

Here is another observation. The script terminates once it changes one of the dropdown lists. In other words, if the first list is already at the default value, it continues, but if the second is not at the default value, it stops there. Or if the first is not at the default, the script stops after changing the first entry. It is as if the script stops executing when it actually changes the value of the dropdown.

 

IMO: Something is broken when executing Javascript as part of the "Action" property of a field. (And yes, I checked if there is an update, I'm on the current version.)

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 ,
Apr 26, 2022 Apr 26, 2022

Change this line:

this.getField(v[i]).value="SSS";

To:

var fname = v[i];
var f = this.getField(fname);
if (f==null) app.alert("Error! Can't find: " + fname);
else f.value="SSS";

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 ,
Apr 26, 2022 Apr 26, 2022

Sorry - Same results.

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 ,
Apr 26, 2022 Apr 26, 2022

Are there any error messages in the JS Console? Can you share your actual file with us?

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 ,
Apr 26, 2022 Apr 26, 2022

No error messages.

 

Is there some way I can send the file directly to you? I would rather not post it.

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 ,
Apr 26, 2022 Apr 26, 2022
LATEST

I'll send you an email (I assume your email address is your username... If it's not, send me a PM here).

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 ,
Apr 26, 2022 Apr 26, 2022

Resetting a field means returning it to its default value.

You should try this:

 

for (i=0; i<4; i++) {this.getField("Student" + i).value = this.getField("Student" + i).defaulValue;}


Acrobate du PDF, InDesigner et Photoshoptographe
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 ,
Apr 26, 2022 Apr 26, 2022

No, we are on the wrong track here. 

 

The problem isn't resetting a dropdown list, which works fine, the problem is that the code is trying to reset three dropdown lists and the loop ends prematurely. I put some alerts in there and the loop execution really ends prematurely.

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 ,
Apr 26, 2022 Apr 26, 2022

It works for me:

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