Skip to main content
Inspiring
July 25, 2023
Answered

Making fields read-only

  • July 25, 2023
  • 1 reply
  • 1798 views

I have the code below on a pdf form with text fields and radio buttons over multiple pages.  The code makes all text fields on the current and prior pages read-only as expected.  However, the radio buttons on all pages are read-only.   How do I change the code to make only the radio buttons on the current and prior pages read-only.

 

 

var currentPageNum = event.target.page;
   
 for (var i = 0; i < this.numFields; i++){
      var currentField = this.getField(this.getNthFieldName(i));

   if (currentField.page <= currentPageNum ||  currentField.type == "radiobutton"){
     currentField.readonly = true;
        }
      }

 

This topic has been closed for replies.
Correct answer defaultarwakfbkwks7

Yes, fields can be excluded using any field property. For example here's a modification that uses the field name.

 

 if ( (currentField.name != "MyField") && (GetFirstFieldPage(currentField) <= currentPageNum)){
     currentField.readonly = true;
 }
      

 

If you want to exclude a group of fields it might be better to create a function for identifying them. Or name the fields in a way that is easy to identify in a single code statement. 

 


I was able to get what I was looking for with the code below.  I'm not sure if it's the best way, but it works.

var currentPageNum = event.target.page;
for (var i = 0; i < this.numFields; i++){
      var currentField = this.getField(this.getNthFieldName(i));
   if (currentField.value != "" && GetFirstFieldPage(currentField) == 0){
     currentField.readonly = true;
     this.getField("field1").readonly = true;
        }
      }

var currentPageNum = event.target.page;
 for (var i = 0; i < this.numFields; i++){
      var currentField = this.getField(this.getNthFieldName(i));
   if (GetFirstFieldPage(currentField) <= currentPageNum &&  GetFirstFieldPage(currentField) != 0){
     currentField.readonly = true;
     currentField.display = display.visible;
        }
      }

1 reply

Thom Parker
Community Expert
Community Expert
July 25, 2023

The problem with this code is that the "field.page" property is only a number value when there is only one widget for the field. If the field has more than one widget it is an array of page numbers. One for each widget. By definition, a radio button group has multiple widgets. If you didn't know, a widget is the "instance" of a specific field, i.e., All fields in a radio button group have the same field name, hence they are all the same field. Each button is a widget of that field. 

 

So the solution is to create a function that returns the page of a field, regardless of the number of widgets. 

function GetFirstFieldPage(oField)
{
    return (typeof(oField.page)=="number")?oField.page:oField.page[0];
}
    

   

Make this a document level function.

Now here's your changed code. 

var currentPageNum = event.target.page;
   
 for (var i = 0; i < this.numFields; i++){
      var currentField = this.getField(this.getNthFieldName(i));

   if (GetFirstFieldPage(currentField) <= currentPageNum ||  currentField.type == "radiobutton"){
     currentField.readonly = true;
        }
      }

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Inspiring
July 25, 2023

Thanks for your response. The radio buttons still locks on all pages.

Thom Parker
Community Expert
Community Expert
July 26, 2023

Did you validate (i.e. test) the new function?

Have you done any debug?

Are any of the radio buttons duplicated on different pages?

Where was the original code placed? 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often