Skip to main content
Participating Frequently
July 3, 2023
Answered

buttonSetIcon for all fields with same name

  • July 3, 2023
  • 1 reply
  • 930 views

I am trying to change the icon for all the fields with the same name, then apply a different icon to the event.target.

I have written the following code. The target gets the icon correctly, but the rest of the fields' icons are not changed, and I'm not sure why.

var icon = this.getField("LabelIcon").buttonGetIcon()
var NoIcon = this.getField("LabelNoIcon").buttonGetIcon()
var oAll = this.getField("DangerCheck");
var aAll = oAll.getArray();

for(var field of aAll)
{
field.buttonSetIcon(NoIcon);//This doesn't work.
}
event.target.buttonSetIcon(icon); //This line works fine.

 

Any idea what might cause this?

This topic has been closed for replies.
Correct answer Thom Parker

This still doesn't seem to work correctly.

I changed the code as suggested and looped through the widgets by numbers:

var icon = this.getField("LabelIcon").buttonGetIcon();
var NoIcon = this.getField("LabelNoIcon").buttonGetIcon();
for(var i=0; i<6; i++)
{
var fieldNum = "Danger."+i;
console.println(fieldNum);
this.getField(fieldNum).buttonSetIcon(NoIcon);
}
event.target.buttonSetIcon(icon);//This targets everything, not just the widget I pressed on

Now, the loop works correctly, but the last line seems to ALSO select ALL the widgets instead of just the one I'm clicking...


These kinds of details are not covered in any of the documentation.  It appears event.target is returning the Field object, rather than the widget object. And there is no way to automatically know which widget is associated with field. 

 

You might want to reconsider and give each of the buttons a unique name. 

For example, "Danger.Num1", "Danger.Num2", etc.  This will provide better control over the process.

 

 

1 reply

Thom Parker
Community Expert
Community Expert
July 3, 2023

What are the full names of the "DangerCheck" buttons?

Have you done any debug in the console window?

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participating Frequently
July 3, 2023

Those are the full names of the buttons 🙂
I did do a debug, there are no errors, it just doesn't work.
I tried using console.println(field) and I get [Object Field]. If I try console.println(field.name) I get "DangerCheck", so I assume it has the right field, though it doesn't add the numbers (in the fields list on the right it automatically changes each field to DangerCheck#0-DangerCheck#5).

Thom Parker
Community Expert
Community Expert
July 3, 2023

The "getArray()" function is for use with fields that use "group" naming. What you have is a bunch of fields with the same name. These are different widgets of the same field. 

So, this line will set all of the "DangerCheck" fields to the same image

 

this.getField("DangerCheck").buttonSetIcon(NoIcon);  

 

To set an individual wiget icon, the code needs to specify just that widget

This code sets the icon for the 2nd widget:

 

this.getField("DangerCheck.1").buttonSetIcon(icon);  

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often