Arrays
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Check the Javascript console for errors.
Copy link to clipboard
Copied
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)
Copy link to clipboard
Copied
See if this will work:
for(var i=1; i<=3; i++)
this.getField("Student"+i).value = "SSS";
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Use it without array.
Copy link to clipboard
Copied
What happens when you use:
this.resetForm (v);
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
OK - Some progress, finally. Here is how I am using this. From the "Actions" for the button.
and it fails. but when I change this to run the Javascript before the reset form,
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.)
Copy link to clipboard
Copied
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";
Copy link to clipboard
Copied
Sorry - Same results.
Copy link to clipboard
Copied
Are there any error messages in the JS Console? Can you share your actual file with us?
Copy link to clipboard
Copied
No error messages.
Is there some way I can send the file directly to you? I would rather not post it.
Copy link to clipboard
Copied
I'll send you an email (I assume your email address is your username... If it's not, send me a PM here).
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
It works for me:

