Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
3

Need java script to allow user input in text field if listbox selection is something else.

Community Beginner ,
Mar 30, 2024 Mar 30, 2024

I'm trying to create a field that is automatically populated if a specific selection is made in a listbox.  If that specific selection is NOT in the listbox then I want to be able to manually add text in that field. 

 

See below.  If I select AVLEASE, FJAUDILEASE or FJMBLEASE in the listbox, I want a field to populate with the word LEASE.  If any other selection is made in my listbox, I want to be able to manually enter text into this field.

 

I'm using the Custom calcuation script box in the text field properties calculate tab.

 

This is what I have and it dosen't work.  I cannot manually enter text in that field:

 

var v = this.getField("LISTBOXDEALER").valueAsString;
if (v=="AVLEASE") event.value = "LEASE";
else if (v=="FJAUDILEASE") event.value = "LEASE";
else if (v=="FJMBLEASE") event.value = "LEASE";
else event.value = "";

event.rc = (v != ""); // this blocks the calc
event.target.readonly = event.rc // This blocks or enables user data entry

 

 

EW96753_0-1711830036566.pngexpand image

 

TOPICS
JavaScript
297
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Mar 30, 2024 Mar 30, 2024
LATEST

The calculate script isn't working for a couple of reasons. 

First, "v" is never an empty string, so event.rc is never set to false and readonly is always true. 

Second, both setting event.rc to false and setting the field value to a blank are not possible, because setting event.rc to false tells the JS engine to ignore event.value. 

 

This type of action is better done in the  "Select Change" script of the listbox.  

Also make sure the "Commit selected value immediately" option is checked. 

Here's the select change script:

var oTgtFld = this.getField("textbox");
if ((event.value=="AVLEASE") || (event.value=="FJAUDILEASE") ||(event.value=="FJMBLEASE")) 
{
    oTgtFld.value = "LEASE";
    oTgtFld.readonly = true;
}
else 
{
    oTgtFld.value = "";
    oTgtFld.readonly = false;
}

 

 

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

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 30, 2024 Mar 30, 2024

To be more clear, I want a textbox to be populated with the result of specific entries in a listbox.  If one of those entrites in NOT selected then I want to be able to manually enter text in the textbox.  In my example if I select one of the three leases, then I want the word LEASE to appear in the text box.  If I choose one of the three other options, then I want to be able to manually enter text in the textbox. With my current script, as soon as I tab off the textbox after entering anything, the text disappears.

 

EW96753_0-1711833262962.pngexpand image

 

 

EW96753_1-1711833415588.pngexpand image

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 30, 2024 Mar 30, 2024
LATEST

The calculate script isn't working for a couple of reasons. 

First, "v" is never an empty string, so event.rc is never set to false and readonly is always true. 

Second, both setting event.rc to false and setting the field value to a blank are not possible, because setting event.rc to false tells the JS engine to ignore event.value. 

 

This type of action is better done in the  "Select Change" script of the listbox.  

Also make sure the "Commit selected value immediately" option is checked. 

Here's the select change script:

var oTgtFld = this.getField("textbox");
if ((event.value=="AVLEASE") || (event.value=="FJAUDILEASE") ||(event.value=="FJMBLEASE")) 
{
    oTgtFld.value = "LEASE";
    oTgtFld.readonly = true;
}
else 
{
    oTgtFld.value = "";
    oTgtFld.readonly = false;
}

 

 

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines