Determine index number of like name checkbox fields using Javascript
Copy link to clipboard
Copied
While one can easily enough view the index for a specific widget or form field by looking at the “Fields” panel in Acrobat, is there a way one can determine the index of several or more like name checkbox fields, i.e., cb.0#0, cb.0#1, cb.0#2, ... using Javascript? While I believe the answer is 'No', I wanted to ask anyway in the event I am overlooking something I may not be aware of. In reality, the only time I have had a need to know the index for several or more checkbox fields all with the same name assigned different export values has been to prevent a user from selecting/checking more than one checkbox by writing scripts that employ the checkbox field methods isBoxChecked(0) and checkThisBox(0) where '0' denotes the '0' based index determined by the order in which the individual widgets of the checkbox field were created. All considered, if the answer to my question cited earlier is 'No' as I suspect, I am somewhat surprised that Adobe never bothered to address this issue not unlike providing a much-needed datepicker twenty years after Adobe Acrobat first debuted back in 1993. Thank you ahead of time.
Copy link to clipboard
Copied
Yes, you can determine the index with the method isBoxChecked.
Copy link to clipboard
Copied
You can determine the index number of a widget based on its location in the array of Field objects that the getArray() method returns.
Copy link to clipboard
Copied
GetArray only returns different field objects within a named group. It doesn't return widgets. In fact, there is no good way to know on which widget an event occurs because event.target is the field object, not the widget object returned when getting a field with the widget number. Radio buttons are special in this respect because there is a value associated with the specific widget. So the value can be used to determine the widget number through the exportValues list. This is also true for radio-checkboxes when they are checked on. But isn't true for any other field type.
For example, try to write a script that sets the fill color for a button widget when it is pushed. This script should be generic and work when copies are made of the button with no scripting change. And don't use the location cheat. i.e., using mouseX and mouseY to find the widget rect.
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
You're right, I thought it did... The other way to do it is to use the "FieldName.X" notation when using getField. So getField("Text1.0") returns the first widget in the group, getField("Text1.1") returns the second, and so on.
Copy link to clipboard
Copied
And the order of these indexes will match the order of the exportValues array, if it's a check-box group.
Copy link to clipboard
Copied
I'm pretty sure that event.target used to match the widget object, so you could use it to set widget properties and to id the widget index. But that definately isn't true now.
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
The answer is Yes. From inside an event script, only if the like named checkboxes have different export values. Then the index is provided by the location of the current value of the field in the field.exportValues array. And the doesn't work if no checkbox is selected.
widget indexes can be determined from location.
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
Thanks to all for the replies to my original post. In reality, no sooner had I posted my question and upon further deliberation came to the same conclusion as posted by Thom Parker in his last reply. What I failed to mention in my initial post is that all like name check boxes have different export values. Unfortunately, the reason behind my initial post was that I wanted to change the userName (tooltip) of each like name checkbox on the fly (three in a row) whenever a user ticked/unticked a checkbox. What I momentarily forgot is that whether a like name checkbox is checked/unchecked, all like name check boxes inherit the same as opposed to retaining different userNames which is what I wanted. While I can get everything to work as intended using checkboxes with different names and implementing the correct scripts whereby only one selection out of three is required each with a different userName (tooltip), I was hoping to be able to accomplish the same task using identical name checkboxes which is not going to happen. The advantage in using like as opposed to all different name checkboxes given 50 rows and three columns of checkboxes is that it would significantly reduce the number of scripts required to achieve the desired result.

