Brendan Jackson
Brendan Jackson

Reputation: 93

Is it possible to select text boxes with javaScript in acrobat

I would like to be able to use the JavaScript function of one of my form inputs to be able to show or hide a text box. After a lot of research I can only find how to select and hide other form input methods.

here is what I have:

var supplyBudget = this.getField("Supply Budget").value;
if (supplyBudget == ""){
/*text box selector*/.style.display = 3;
}
else if (supplyBudget =="0"){
/*text box selector*/.style.display = 3;
}
else{
/*text box selector*/.style.display = 1;
}

This runs when the user leaves the input field.

*edited code in accordance with freginold's comment

Upvotes: 0

Views: 2136

Answers (3)

Jonathan Allen
Jonathan Allen

Reputation: 1

The only way that I have found to "Hide plain Text" and a text field, is to use something similar to @joelgeraci answer. I had to add another empty read only textbox to cover the text and textbox to hide.

Such as:

this.getField("HiddenField").display = display.visible;

Then when I need to "show" the text and text box I would simply hide the hidden field:

this.getField("HiddenField").display = display.hidden;

I have multiple cases of this in a form that I have been creating for work and a lot of JS behind it. I am no expert, just learning as I go.

Upvotes: -1

joelgeraci
joelgeraci

Reputation: 4927

If you want to hide text that is part of the page content, you can't do that with PDF unless that particular text item is assigned to an Optional Content Group (OCG)... basically a layer that you can show or hide. There is no concept of "elements" for PDF page context like there is in HTML. Instead, there's a list of painting instructions for the page but JavaScript does not have access to it.

Upvotes: 1

joelgeraci
joelgeraci

Reputation: 4927

There are no "selectors" in Acrobat JavaScript. However, you can get the number of fields and then iterate to get the names in order to find the ones that are of interest. For example if I wanted to hide all fields where the name starts with "name", I might write...

for ( var i = 0; i < this.numFields; i++) {
    var field = this.getField(this.getNthFieldName(i));
    if ( field.name.indexOf("name") > -1 ) {
      field.display = display.hidden
    }
}

In general, to hide a field use...

this.getField("myFieldName").display = display.hidden;

To show a hidden field use...

this.getField("myFieldName").display = display.visible;

Upvotes: 1

Related Questions