Trying to use FOR and IF to count specific fields on a page

Community Beginner ,
Apr 08, 2020

Copy link to clipboard

Copied

Hello,

 

I am trying to get a field on each page to show a total of the number of buttons on that field's page that have a specific caption ( "X," "(X)," or "OUT OF TIME"). My goal is to be able to paste a copy of this field on each page with a unique name and have it work without needing to change the code. So far my field just displays "1" whether the criteria are met or not, and doesn't update when I change the button captions. (The buttons are set to cycle through a set of captions when I click on them.) By default, all buttons' captions are "", but after clicking, the captions will cycle through "X," "(X)," "OUT OF TIME," "OK," and back to "".

 

I'm not great with "for" loops yet, so I'm not sure what I'm missing.

	for (var i = 0; i < this.numFields; i++) {
		var f = this.getField(this.getNthFieldName(i));
		var reg = /btnOriginal/;
		var count = 0;
		
		if (f.pageNum == event.target.pageNum && f.name.match(reg) && f.buttonGetCaption == "X" || "(X)" || "OUT OF TIME") {
		count++
		};
	}; 
	
event.value = count;

 Any help is greatly appreciated!

TOPICS
Acrobat SDK and JavaScript, PDF forms

Views

22

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

Trying to use FOR and IF to count specific fields on a page

Community Beginner ,
Apr 08, 2020

Copy link to clipboard

Copied

Hello,

 

I am trying to get a field on each page to show a total of the number of buttons on that field's page that have a specific caption ( "X," "(X)," or "OUT OF TIME"). My goal is to be able to paste a copy of this field on each page with a unique name and have it work without needing to change the code. So far my field just displays "1" whether the criteria are met or not, and doesn't update when I change the button captions. (The buttons are set to cycle through a set of captions when I click on them.) By default, all buttons' captions are "", but after clicking, the captions will cycle through "X," "(X)," "OUT OF TIME," "OK," and back to "".

 

I'm not great with "for" loops yet, so I'm not sure what I'm missing.

	for (var i = 0; i < this.numFields; i++) {
		var f = this.getField(this.getNthFieldName(i));
		var reg = /btnOriginal/;
		var count = 0;
		
		if (f.pageNum == event.target.pageNum && f.name.match(reg) && f.buttonGetCaption == "X" || "(X)" || "OUT OF TIME") {
		count++
		};
	}; 
	
event.value = count;

 Any help is greatly appreciated!

TOPICS
Acrobat SDK and JavaScript, PDF forms

Views

23

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
Apr 08, 2020 0
Adobe Community Professional ,
Apr 08, 2020

Copy link to clipboard

Copied

First, Did you look in the console window to see if any errors were reported?

 

One of the first issues is that count is set to 0 everytime around the loop, so it will never be more than 1. 

Next, move the RegExp definition out side the loop. It only needs to be defined once.

 

Next, your "if" conditions need some help.  There is a lot going on here, and I can see that you didn't really test any of this separately to see what works and what doesn't. And every part of it has a problem.

1. If there is more than one field with the same name, then the page number is an array, not a number. Do you have buttons that have the same name?

2.  Use the RegExp.test() function rather than the match.

3.  f.buttonGetCaption is a function, not a property

4. f.buttonGertCaption only works for buttons. Are there any fields on the form that are not buttons? If so the script also needs to test for (f.type == "button")

5. You cannot compare multiple things using (a == b || c || d). It needs to be written (a==b) || (a==c) || (a==d)    

 

What you need to do is use the Console Window to test these things before ever trying them in a field script

Watch the console window tutorial here:

https://www.pdfscripting.com/public/Free_Videos.cfm#JSIntro

 

 

 

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...
Apr 08, 2020 1
Community Beginner ,
Apr 08, 2020

Copy link to clipboard

Copied

Thank you Thom. The console error I was getting was "f is null."  I'll go back and change the things you mentioned and start testing it in pieces in the console window.

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...
Apr 08, 2020 1