Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Participant ,
May 10, 2016 May 10, 2016

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!

TOPICS
Acrobat SDK and JavaScript , Windows
1.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , May 11, 2016 May 11, 2016

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 (th

...
Translate
Community Expert ,
May 10, 2016 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 11, 2016 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

Function.jpg

And here is where I have called the function

FunctionCall.jpg

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 11, 2016 May 11, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 11, 2016 May 11, 2016

Yes on Mouse Up it Runs a JavaScript, the button it's self is only there to be clicked and none of the fields inside the function relate to the button, only text fields are changed or modified.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 11, 2016 May 11, 2016

Good. And when you say that there are no errors, do you mean that there are no error popular messages, or are you actually checking the JavaScript console for errors? The console can be displayed via Ctrl-J or Cmd-J

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 11, 2016 May 11, 2016

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 11, 2016 May 11, 2016

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 11, 2016 May 11, 2016

Thanks for this, I was wondering if I should have split them up into two levels and was in the process of trying to do that, I'll modify the code again and see if it runs.

Edit: Okay it works perfectly now that I have split the if statements up.

In the end I made 3 levels of if statements, one for the isNaN, then one for "inc" and one for "dec" and it properly increments and decrements the values without allowing them to run into negatives.

Here's what it looks like

Function.jpg

Thank you very much for your help Karl!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 19, 2016 May 19, 2016

Just a quick question;

Is there a way I can write the parameters of a function on multiple lines?

Say I have something like:

function fnName (op1, op2, op3, op4, op5... ,op35) {

//do something

}

That many parameters would make the line quite long, long enough to go off the side of the screen, although not a major issue I'd rather not have to horizontal scroll every time I wish to fix/add something to the parameters.

So would there be a way to write it more like this:

function fnName (op1, op2, op3, op4, op5...,

op15, op16, op17. op18, op19, op20..., op30) {

//do something

}

Every way I've tried to split these onto multiple lines it has caused the function to no longer work.

I'm beginning to wonder if it's even possible.

Apologies if this is a basic question but I can't find any examples anywhere.

Thank you in advance.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 19, 2016 May 19, 2016

Splitting up the argument list into separate lines is perfectly legal JavaScript. The only requirement is that parameters are separated by commas. In your case, it looks like there is a period after "op17", and not a comma.

What exactly do you mean by "no longer work"? Are you getting errors on the JavaScript console? What happens when you step through your code in the JavaScript debugger?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 19, 2016 May 19, 2016

Well, when they previously functioned with no error I break the line up and after that it does not do what it would previously, again without an error.

I was unsure whether or not it was because I was missing an operator or not but if you say it's possible with just making sure the commas are in the correct places then I'll have another go with it and see if I made a mistake somewhere.

If it doesn't work I'll post the actual script as the one above was just an example.

Edit: It has worked this time, I must have removed or changed a comma somewhere, thank you anyway Karl!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jun 06, 2016 Jun 06, 2016

Just another question, am I able to use Global Variables as arguments in a function?

Every time I've tried to use it, they have come back as undefined.

From what I've looked at using global variables is not a good idea but those are usually with reference to web based JavaScript.

Maybe I'm doing something wrong but I can't find any examples for what I'm trying to do.

Here's a simplified version of what I'm using in my document, this comes back undefined.

Inside Document Level:Variables;

gblVar1 = "On";

gblVar2 = "Off";

Inside Document Level:globalCall;

function globalCall(Global) {

     var fldTxt1 = this.getField("Text1");

     fldTxt1.value = Global.value;

}

On event: MouseUp: Run A JavaScript;

Inside Button1;

globalCall(gblVar1);

Inside Button2;

globalCall(gblVar2);

The text field Text1 comes back as undefined.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 06, 2016 Jun 06, 2016

Use this:

  1. function globalCall(Global) { 
  2.      var fldTxt1 = this.getField("Text1"); 
  3.      fldTxt1.value = Global; 
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jun 06, 2016 Jun 06, 2016
LATEST

Ahh I see it's the .value after Global causing the issue, thank you!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines