Skip to main content
Inspiring
May 10, 2016
Answered

Is there a way to modify this script to be reuseable in a function?

  • May 10, 2016
  • 1 reply
  • 1939 views

Hello,

I have this script inside a load of buttons (Currently 16 individual buttons)

They all have a different value and some of them increment positively and some negatively they are + - buttons

Here is the the Script:

var fldTI1 = this.getField("20mmLeft");

if (isNaN(fldTI1.value)) {

fldTI1.value = 0;

}

else {

fldTI1.value += 1;

this.getField("LeftAddOnTotal").value += 20;

}

I'm wondering if I can make this more universal so that I only have to write it in a function and call that function?

I know I will most likely have to write 2 functions for each value (15, 20, 35 and 50), one for the positive increment and one for the negative increment.

I was thinking something along the lines of using var fldT1 = this.getField(event.target.name); or something like that so that the var pulls the name from where the function was called?

I've tried looking into event.target syntax and what it does but I can't find a clear explanation as to what it actually does.

Any help and suggestions would be greatly appreciated.

Thank you in advance!

This topic has been closed for replies.
Correct answer Karl Heinz Kremer

I checked the console, at first it said that inc was not defined because I had forgotten the " " around it, after that I ran it again and still nothing happened, so I checked the console again and it was clear of any error messages.

Edit: I've also checked that all the field names are correct aswell.

Edit2: I changed the else if statements to simply if statements and it works, however it also runs the else statement aswell.


I general, to debug a problem like this, I would add the following line at the top of the function:

app.alert("addOnValues: " + name + " " + name2 + " " + op + " " + amount);

This will print all the parameters passed into the function, and will show you that the function actually got called. Alternatively, you can use the JavaScript debugger and set a breakpoint on the first line of this function, and then single step through the code.

However, in this specific case, I can tell you what's wrong (the first time I looked at your code, I only had my cell phone, now that I am working on the computer, it's easier to see what's going on): The if/else if construct you are using has the same conditions multiple times:

if (op == "inc") {

  // ...

}

else if (op == "inc") {

  // ...

}

In an if/else if/else construct, only the first condition that matches will be executed. After that, the interpreter jumps to the end of the construct, and executes the next command. You will need to create if statements on two levels:

if (op == "inc") {

    if (isNaN(fldName.value)) {

      // ...

    }

   else {

     // ...

   }

}

else if (op == "dec") {

   if (isNaN(fldName.value)) {

     // ...

   }

  else {

     // ...

  }

}

else {

     // ...

}

You can simplify this a bit more because you know that the action for the "isNaN" case is always the same, so you could use this:

if (isNaN(fldName.value)) {

}

else if (op == "inc") {

}

else if (op == "dec") {

}

else {

}

Again, this is just normal JavaScript and has nothing to do with Acrobat's JavaScript implementation. You may want to read up on the core JavaScript syntax.

1 reply

Karl Heinz  Kremer
Community Expert
Community Expert
May 10, 2016

You can certainly do that. Keep in mind that a function can take more than just one argument. In your example, you are only passing one argument ("event.target.name" as the field name). Let's see what information you need to do the job in this function: Based on your sample script, you are not using the name of the field that was clicked, you are using the names of two other fields. However, I assume that the name of the first field ("fldT1") is based on your button name: Let's say you have two buttons to increment and decrement the value in a field, and the field is called "fldT1", the increment button is called "btnIncT1", and the decrement button is called "btnDecT1". In this case, if you know which button was pressed, you can determine which field needs to be modified, and also by what operation, because your button name is always "btw" followed by either "Inc" or "Dec", followed by the field name. You can use this knowledge to analyze the name and determine both the operation and the target field name. That does add a bit of complexity, and it might be easier to come up with a more direct approach.

Alternatively, you can create a function that takes three parameters: The target field name, the operation, and the amount:

function doSomething(name, op, amount) {

  // do something

}

This would work, but you may find out that calling "this.getField("someName")" in your script might be unreliable: The "this" pointer may not point to the current document anymore, depending on the scope of your function call. Because of that, it's a good idea to pass in the document as well as another parameter:

function doSomething(doc, name, op, amount) {

  // do something

   var f = doc.getField(name);

}

You would then call the function from your button handler like this:

doSomething(this, "fldT1", "inc", 50);

This assumes that you test for the operation the like this in your function:

if (op == "inc") {

  // do someting

}

else if (op == "dec") {

  // do something else

}

else {

  // ERROR !

  app.alert("invalid operation type");

}

This is actually basic JavaScript, and has very little to do with how JavaScript works in Acrobat.

Inspiring
May 11, 2016

Thank you for you answer Karl, I've had a go at writing the alternative function but I think I've gone wrong somewhere,

Here is what I have written

And here is where I have called the function

I added an extra parameter (name2) for the second field that I call which is the total sum of what's been added/removed

It runs without an error but nothing happens not even the app.alert and I can't figure out why.

I feel it may be something simple or the simple fact I've written it completely wrong I don't know.

Karl Heinz  Kremer
Community Expert
Community Expert
May 11, 2016

From what context are you calling the function? Is it a mouse up handler on your buttons?