Skip to main content
JMFreeman
Inspiring
Jan 15, 2020 3:31 PM UTC
Question

Getting a Field Name With a Generic Number

For my project, I have pages spawn from a template. I use a hidden form field containing a count variable which I append to the names of spawned fields and increment. There are fields such as topPh#, topCap#, etc. These numbers may or may not match with page numbers or be in sequential order.

 

  • How can I make a loop that will go to each page and collect topPh# etc. regardless of the integer there? I also would want to have a list of the # values in order.

 

I used regular expressions to do something similar:

 

var num = event.target.name.match(/\d/g).join('');

this.getField("botPh" + num).display = (event.target.value=="Off") ? display.hidden : display.visible;

this.getField("botCap" + num).display = (event.target.value=="Off") ? display.hidden : display.visible;

 

But in the above case, my target widget has a number on it (e.g. togBot5), and it talks to other widgets with the same number (e.g. botPh5), regardless what the number is.

This topic is closed to new replies. Start a new post to keep the conversation going.

11 replies

JMFreeman
JMFreemanAuthor
Inspiring
Jan 15, 2020 7:43 PM UTC

I'm getting closer. I need to modify the following:

     var fields = [];
     for (var i = 0; i < this.numFields; i++){
          var f = this.getField(this.getNthFieldName(i)).name;
          if(f == "topPh2") fields.push(f);
     }     //end of for loop
     app.alert(fields);

 Instead of

if (f == "topPh2") fields.push(f);

 I need something for any name beginning with "topPh". 

Translate
JMFreeman
JMFreemanAuthor
Inspiring
Jan 15, 2020 7:46 PM UTC

Look like this works

     var fields = [];
     for (var i = 0; i < this.numFields; i++){
          var f = this.getField(this.getNthFieldName(i)).name;
          if(f.indexOf("topPh") > -1) fields.push(f);
     }     //end of for loop
     app.alert(fields);

I was avoiding indexOf. Is there an alternative? 

Translate
Bernd Alheit
Community Expert
Community Expert
Jan 15, 2020 8:11 PM UTC

You can also use the search method.

Translate
JMFreeman
JMFreemanAuthor
Inspiring
Jan 15, 2020 5:45 PM UTC

I am thinking I need to make a function that I can pass a page number, and it figures out the field names on that page. Is there a way to use getField on a certain page maybe?

Translate
Bernd Alheit
Community Expert
Community Expert
Jan 15, 2020 5:50 PM UTC

Use getField and check the property page of the field.

Translate
JMFreeman
JMFreemanAuthor
Inspiring
Jan 15, 2020 5:59 PM UTC

How can I use getField wihtout knowing the number that the field name will end with?

Translate
Bernd Alheit
Community Expert
Community Expert
Jan 15, 2020 3:44 PM UTC

Check the property page of the field.

Translate
JMFreeman
JMFreemanAuthor
Inspiring
Jan 15, 2020 5:36 PM UTC

I'm sorry, it seems my intent is unclear.

 

Each page of my document has a top photo and a bottom photo. I'm working on buttons/features for the document to help manipulate the order of the photos. If I have 3 pages, so 6 photos total, and I want to delete photo 3 (which is the top photo of page 2), I plan to fill an array using buttonGetIcon for all the photos past photo 3. Then I can use buttonSetIcon and replace photo 3 with photo 4, replace 4 with 5, etc. (I'm going to worry about what happens to the last one later. It's much less disruptive to the user than deleting in the middle of the document.)

 

A rub to the problem is the names of the photo button fields. They are all named "topPh" or "botPh" with a unique number at the end. The number may not match the page number or be in sequence with the last number. I need to make a loop that will fill an array with the icons from each photo, starting at photo 3 and going to the end. In the following code, I need to figure out how to make the for loop find the fields, regardless of their number.

//pop up a confirmation window; yes = 4, no = 3
var confirm = app.alert("Are you sure you want to delete this photo?\nThis cannot be undone.", 1, 2, "Confirm Photo Deletion");

if(confirm == 4){       //this only runs if they say "yes", otherwise nothing happens.
     var pg = event.target.page;
     var aPg = this.numPages - pg;
     var icons = []
     for(var i = 0, i < aPg, i++){
          icons[i] = ???
     }     //end of for loop
}     //end of if statement

 The ??? is where I need to make it find the field "topPhX", for any value of X (as well as "botPhX"), then go the next page and find its fields, "topPhY" and "botPhY", where there is not necessarily a relationship between X and Y.

Translate