Change borderColor fields in Array

Explorer ,
Aug 01, 2018

Copy link to clipboard

Copied

I would like to change the border color of all of the fields in a 60+ array of fields, dependent upon whether a button is hidden, without having to set each field border color one-by-one. Here is an example of how I have this set up, which is not working. How can I do this?

var btn = this.getField("btn.ABC");

var highlight = new Array()

    highlight[0] = "chk.BP"

    highlight[1] = "txt.CD.A.Line.01.b"

    highlight[2] = "num.CD.BPAC.A.01"

    highlight[3] = "num.CD.BPBC.A.01"

    highlight[4] = "txt.CD.A.02"

    highlight[5] = "num.CD.BPAC.A.02"

if(btn.hidden == true){highlight.borderColor = color.white}else {highlight.borderColor = color.black}

Adobe Community Professional
Correct answer by gkaiseril | Adobe Community Professional

Within a script one can tell if the "getField" failed by checking to see if the returned variable of object the method is a null value or not. Since your array cells consist of the field object for each field, just use the cell's contents to access the field's value.

var btn = getField("btn.Defaults.Loan");

var highlight = new Array(

"chk.BP"

"txt.CD.A.Line.01.b"

"num.CD.BPAC.A.01",

"num.CD.BPBC.A.01",

"txt.CD.A.02"'

"num.CD.BPAC.A.02",

"num.CD.BPBC.A.02",

"txt.CD.A.03",

"num.CD.BPAC.A.03",

"num.CD.BPBC.A.03",

"txt.CD.A.04”

)

//this continues, naming 63 fields in this manner

for (var i in highlight) {

var f = this.getField(highlight);

if(f == null) {

  console.show();

  app.alert("Error accessing field " + highlight, 1, 0, "Field Access Error");

} // end if field object null;

  if (btn.hidden == true) {

  f.strokeColor = color.cyan;

} else {

  f.strokeColor = ["RGB", 1, .8, .05];

  f.fillColor = ["RGB", .92, .92, .92];

} // end if;

} // end for loop;

TOPICS
Acrobat SDK and JavaScript, Macintosh

Views

297

Likes

Translate

Translate

Report

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

Change borderColor fields in Array

Explorer ,
Aug 01, 2018

Copy link to clipboard

Copied

I would like to change the border color of all of the fields in a 60+ array of fields, dependent upon whether a button is hidden, without having to set each field border color one-by-one. Here is an example of how I have this set up, which is not working. How can I do this?

var btn = this.getField("btn.ABC");

var highlight = new Array()

    highlight[0] = "chk.BP"

    highlight[1] = "txt.CD.A.Line.01.b"

    highlight[2] = "num.CD.BPAC.A.01"

    highlight[3] = "num.CD.BPBC.A.01"

    highlight[4] = "txt.CD.A.02"

    highlight[5] = "num.CD.BPAC.A.02"

if(btn.hidden == true){highlight.borderColor = color.white}else {highlight.borderColor = color.black}

Adobe Community Professional
Correct answer by gkaiseril | Adobe Community Professional

Within a script one can tell if the "getField" failed by checking to see if the returned variable of object the method is a null value or not. Since your array cells consist of the field object for each field, just use the cell's contents to access the field's value.

var btn = getField("btn.Defaults.Loan");

var highlight = new Array(

"chk.BP"

"txt.CD.A.Line.01.b"

"num.CD.BPAC.A.01",

"num.CD.BPBC.A.01",

"txt.CD.A.02"'

"num.CD.BPAC.A.02",

"num.CD.BPBC.A.02",

"txt.CD.A.03",

"num.CD.BPAC.A.03",

"num.CD.BPBC.A.03",

"txt.CD.A.04”

)

//this continues, naming 63 fields in this manner

for (var i in highlight) {

var f = this.getField(highlight);

if(f == null) {

  console.show();

  app.alert("Error accessing field " + highlight, 1, 0, "Field Access Error");

} // end if field object null;

  if (btn.hidden == true) {

  f.strokeColor = color.cyan;

} else {

  f.strokeColor = ["RGB", 1, .8, .05];

  f.fillColor = ["RGB", .92, .92, .92];

} // end if;

} // end for loop;

TOPICS
Acrobat SDK and JavaScript, Macintosh

Views

298

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Aug 01, 2018 0
Most Valuable Participant ,
Aug 01, 2018

Copy link to clipboard

Copied

Several issues there:

1. That's not how arrays work. You need to use a for-loop and iterate over each item in it.

2. You can't use the name to access the field, you have to do it via the getField method.

3. Fields don't have a borderColor property. The name of this property is strokeColor.

So use this code:

for (var i in highlight) {

    var f = this.getField(highlight);

    if (btn.hidden == true) { f.strokeColor = color.white; } else { f.strokeColor = color.black; }

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 01, 2018 0
Explorer ,
Aug 01, 2018

Copy link to clipboard

Copied

Thank you very much.

I follow what you’re telling me, however, I’m getting an “f is null” error message.

Am I still not identifying my fields correctly? Here’s what I’ve done:

var btn = getField("btn.Defaults.Loan");

var highlight = new Array();

highlight[0] = this.getField("chk.BP");

highlight[1] = this.getField("txt.CD.A.Line.01.b");

highlight[2] = this.getField("num.CD.BPAC.A.01");

highlight[3] = this.getField("num.CD.BPBC.A.01");

highlight[4] = this.getField("txt.CD.A.02");

highlight[5] = this.getField("num.CD.BPAC.A.02");

highlight[6] = this.getField("num.CD.BPBC.A.02");

highlight[7] = this.getField("txt.CD.A.03");

highlight[8] = this.getField("num.CD.BPAC.A.03");

highlight[9] = this.getField("num.CD.BPBC.A.03");

highlight[10] = this.getField("txt.CD.A.04”);

//this continues, naming 63 fields in this manner

for (var i in highlight) {

var f = this.getField(highlight);

if (btn.hidden == true) { f.strokeColor = color.cyan;} else { f.strokeColor = ["RGB", 1, .8, .05]; f.fillColor = ["RGB", .92, .92, .92];}

}

David

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 01, 2018 0
Most Valuable Participant ,
Aug 01, 2018

Copy link to clipboard

Copied

That means one of your field names is incorrect.

On 2 August 2018 at 01:07, ddunn@davidsdunn.com <forums_noreply@adobe.com>

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 01, 2018 0
Engaged ,
Aug 02, 2018

Copy link to clipboard

Copied

And to quickly identify which one, add an alert into your for loop that indicates the value of i in the console.  You will instantly find which item in the array is wrong.

  1. for (var i in highlight) { 
  2.     console.println("\rLast attempt was item no "+i)
  3.     var f = this.getField(highlight); 
  4.     if (btn.hidden == true) { f.strokeColor = color.white; } else { f.strokeColor = color.black; } 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 02, 2018 0
Adobe Community Professional ,
Aug 02, 2018

Copy link to clipboard

Copied

Within a script one can tell if the "getField" failed by checking to see if the returned variable of object the method is a null value or not. Since your array cells consist of the field object for each field, just use the cell's contents to access the field's value.

var btn = getField("btn.Defaults.Loan");

var highlight = new Array(

"chk.BP"

"txt.CD.A.Line.01.b"

"num.CD.BPAC.A.01",

"num.CD.BPBC.A.01",

"txt.CD.A.02"'

"num.CD.BPAC.A.02",

"num.CD.BPBC.A.02",

"txt.CD.A.03",

"num.CD.BPAC.A.03",

"num.CD.BPBC.A.03",

"txt.CD.A.04”

)

//this continues, naming 63 fields in this manner

for (var i in highlight) {

var f = this.getField(highlight);

if(f == null) {

  console.show();

  app.alert("Error accessing field " + highlight, 1, 0, "Field Access Error");

} // end if field object null;

  if (btn.hidden == true) {

  f.strokeColor = color.cyan;

} else {

  f.strokeColor = ["RGB", 1, .8, .05];

  f.fillColor = ["RGB", .92, .92, .92];

} // end if;

} // end for loop;

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 02, 2018 0
Most Valuable Participant ,
Aug 03, 2018

Copy link to clipboard

Copied

Why did you change the code? Either you use getField in the for-loop or you use it in the definition of the items in the array. You can't use it in both... I think it makes more sense to have an array of just the field-names and then use getField in the loop, personally.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 03, 2018 0
ODuinn LATEST
Explorer ,
Aug 03, 2018

Copy link to clipboard

Copied

Sorry for my slow response.

To answer your question, I changed the code because I misunderstood what you meant by "You can't use the name to access the field, you have to do it via the getField method."

When I modify my original iteration with your suggestion in the following manner, this too works. gkaiseril's answer is simpler (eliminates tagging the variable name to each field name line-by-line) and it provides a simple means to identify an errant field name

var btn = getField("btn.Defaults.Loan");

var highlight = new Array();

    highlight[0] = "chk.BP",

    highlight[1] = "txt.CD.A.Line.01.b",

    highlight[2] = "num.CD.BPAC.A.01",

    highlight[3] = "num.CD.BPBC.A.01",

    highlight[4] = "txt.CD.A.02",

    highlight[5] = "num.CD.BPAC.A.02",

    highlight[6] = "num.CD.BPBC.A.02",

    highlight[7] = "txt.CD.A.03",

    highlight[8] = "num.CD.BPAC.A.03",

    highlight[9] = "num.CD.BPBC.A.03",

    highlight[10] = "txt.CD.A.04”

//this continues, naming 63 fields in this manner

for (var i in highlight) {

    var f = this.getField(highlight);

    if (btn.hidden == true) { f.strokeColor = color.cyan;} else { f.strokeColor = ["RGB", 1, .8, .05]; f.fillColor = ["RGB", .92, .92, .92];}

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 03, 2018 0