Skip to main content
Piotr Smolen
Inspiring
December 15, 2022
Question

How to delete only one text field out of many copies using a JS

  • December 15, 2022
  • 2 replies
  • 1761 views

If I add a field with the same name twice using the script and method

this.addField("name", …… )

then in the field collection I will have two fields named "name#0" and "name#1", which correlates with the values order property in the field collection.

Calling this.removeField("name") will unfortunately delete both fields.

Is it possible to delete only the field, e.g. "name#0", of course, using a script?

This topic has been closed for replies.

2 replies

Thom Parker
Community Expert
Community Expert
December 15, 2022

If those are the names being displayed on the field list in "Prepare Form" mode, then you don't have two fields. You have one field with two widgets.  #0 and #1 are the widget numbers.

The proper way to access a single widget of a field is with dot notation. 

 

this.getField("name.0") 

 

gets the object for "name#0".  Use can use this to set field widget parameters, such as the fill color. Widget parameters are all those that are related to the appearance of the field on the page, and have nothing to do with the field level properties, such as the field value.

 

Unfortunately this notation does not allow you to delete the specific widget using the "removeField" function. 

A work around would be to collect all the field parameters for the one you want to keep, delete the field, then recreate it. 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Piotr Smolen
Inspiring
December 16, 2022

Of course I use the documentation. The two most important scripting manuals in Adobe are actually open all the time. However, some answers I will not find in them. The second thing, but this is my problem, but probably not only, English is not my native language, and sometimes some nuances are hard for me to understand. I belong to the generation of Poles who were forced to learn the Russian language, and English appeared only in college and in the basics. Such a digression.

As for the previous suggestion regarding a different approach to creating fields, in this case I want to have fields with exactly the same name before and after changes, because these changes can occur quite often, and as you have just made sure me, it is impossible to delete a specific widget. In addition, it is not possible to programmatically set the TAB order in detail and it is also not possible to move (without deleting) the fields between the pages of the form. So the only reasonable option is "read the properties of the old field, delete it, create a new one in the new place, give the remembered properties". And that's what I did and it works…. and then find out that for security reasons you can't add a blank page with the script ..... 😞

try67
Community Expert
Community Expert
December 16, 2022

Why do you need to add a blank page to do that?

BarlaeDC
Community Expert
Community Expert
December 15, 2022

Hi,

 

I have not been able to find a method to delete the fields using JavaScript, but are you able to change how you create the fields so that you create a count of the fields, then you could create the fields as

this.addField("name." + count, .........)

This allows you to manage all the fields with

this.getField("name");

or get a specific one with

this.getField("name.1")

 

hope this helps