Skip to main content
JMFreeman
Inspiring
January 22, 2020
Answered

Why am I getting a TypeError?

  • January 22, 2020
  • 3 replies
  • 690 views

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

This topic has been closed for replies.
Correct answer JMFreeman

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.

3 replies

JMFreeman
JMFreemanAuthorCorrect answer
Inspiring
January 22, 2020

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.

JMFreeman
JMFreemanAuthor
Inspiring
January 22, 2020

Is using 'f' as a variable in both places a problem?

Bernd Alheit
Community Expert
Community Expert
January 22, 2020

What is the code of the function getFieldOn?

JMFreeman
JMFreemanAuthor
Inspiring
January 22, 2020
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;
    }
}