Copy link to clipboard
Copied
I have a document level script for updating my page numbers:
function updatePg(){
var f;
for(var p = 0; p < this.numPages; p++){
f = getFieldOn("pgN", p);
f.value = "Page " + (p + 1) + " of ";
this.getField("pgT").value = this.numPages;
}
}
I then have it called from a button that spawns pages. It was working, but later without changing that code, I started getting
TypeError: f is undefined
6:Document-Level:updatePg
I figured it out. I don't have the pgN field on my template, and when it's unhidden for me to edit it, the function doesn't work. I need to add an if statement to see if the field exists before trying to change it.
Copy link to clipboard
Copied
What is the code of the function getFieldOn?
Copy link to clipboard
Copied
function getFieldOn(name, p){//finds (the first) field on page p with 'name' in its name
var fields;
for(var i = 0; i < this.numFields; i++){
var f = this.getField(this.getNthFieldName(i));
if ((f.name.indexOf(name) > -1) && (f.page == p)) return f;
}
}
Copy link to clipboard
Copied
getFieldOn is also a document level function. Is this an issue?
Copy link to clipboard
Copied
Is using 'f' as a variable in both places a problem?
Copy link to clipboard
Copied
I figured it out. I don't have the pgN field on my template, and when it's unhidden for me to edit it, the function doesn't work. I need to add an if statement to see if the field exists before trying to change it.