Skip to main content
jasperwt
Participant
February 7, 2017
Answered

Getting Error 'indexOf is not a function' - Acrobat X

  • February 7, 2017
  • 3 replies
  • 1085 views

Hello,

I have a document level javascript that is looking for character strings to determine a result.

Why would it throw this error?


function webaddress() {
          var one = this.getField("CompanyName");
          var two = this.getField("WebAddress");
          var three = this.getField("dealerCSZ");
          var n = three.indexOf("MN,");
          var n1 = three.indexOf("IN,");
          var n2 = three.indexOf("OH,");

        // evaluate address for proper name

         if (n.value>=1)
              {
             one.value='TRUCK COUNTRY USED TRUCK CENTER';   
             two.value='www.truckcountry.com'; 
              }
         else if (n1.value>0 || n2.value>0)
              {
             one.value='STOOPS FREIGHTLINER - QUALITY TRUCKS';   
             two.value='www.stoops.com'; 
              }
         else  
              {
             one.value='TRUCK COUNTRY';   
             two.value='www.truckcountry.com'; 
              }

}
webaddress(); // call my function

This topic has been closed for replies.
Correct answer Bernd Alheit

Use:

if (n >= 1)

Info: the variables n, n1, and n2 are local variables of the function.

3 replies

jasperwt
jasperwtAuthor
Participant
February 7, 2017

You are absolutely correct!

Thanks for your help.

Works great.

jasperwt
jasperwtAuthor
Participant
February 7, 2017

All - thanks for the quick reply.

I added 'valueAsString' and am no longer getting an error.

However, the variables n, n1, and n2 are all showing 'undefined' when I print them out.

Is the indexOf method able to evaluate a comma?

Here is the current script.

function webaddress() {
          var one = this.getField("CompanyName");
          var two = this.getField("WebAddress");
          var three = this.getField("dealerCSZ");
          var n = three.valueAsString.indexOf("MN,");
          var n1 = three.valueAsString.indexOf("IN,");
          var n2 = three.valueAsString.indexOf("OH,");

        // evaluate address for proper name

         if (n.value>=1)
              {
             one.value='TRUCK COUNTRY USED TRUCK CENTER';   
             two.value='www.truckcountry.com'; 
              }
         else if (n1.value>0 || n2.value>0)
              {
             one.value='STOOPS FREIGHTLINER - QUALITY TRUCKS';               two.value='www.stoops.com'; 
              }
         else  
              {
             one.value='TRUCK COUNTRY';   
             two.value='www.truckcountry.com';
              }

}
webaddress(); // call my function

Bernd Alheit
Community Expert
Bernd AlheitCommunity ExpertCorrect answer
Community Expert
February 7, 2017

Use:

if (n >= 1)

Info: the variables n, n1, and n2 are local variables of the function.

Bernd Alheit
Community Expert
Community Expert
February 7, 2017

You must use the value of the field. Example:

var n = three.value.indexOf("MN,");

Karl Heinz  Kremer
Community Expert
Community Expert
February 7, 2017

Actually, you should use the valueAsString property instead of value - otherwise the code will throw an exception if the value is not already a string:

var n = three.valueAsString.indexOf("MN,");