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

How to refer to dropdown not by name

New Here ,
Jan 24, 2018 Jan 24, 2018

Copy link to clipboard

Copied

I have a series of dropdowns that I want to attach the same script to that basically makes a "notes" textfield required if the dropdown value is "fail" (and makes it not required if it is "pass"). The script I have for one of the dropdowns is:

var result = this.getField("Dropdown5");

var notes = this.getField("Text14");

if(result.value == "Fail"){

    notes.required = true;

}else{

    notes.required = false;

}

on mouse exit and it works fine but I was wondering if there is some other way to reference the dropdown that the script is attached to besides using the name. I know this refers to the pdf, but is there some other javascript construct or something to refer to the currently attached field? This way I won't have to edit a bunch of different scripts just to change the name and I can just do a copy/paste. OR is there some way to do this with a document script? I tried making one but nothing happened when I changed any dropdowns.

TOPICS
Acrobat SDK and JavaScript , Windows

Views

341

Translate

Translate

Report

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

correct answers 1 Correct answer

Community Expert , Jan 24, 2018 Jan 24, 2018

You can refer to the field using this code: event.target

Votes

Translate

Translate
LEGEND ,
Jan 24, 2018 Jan 24, 2018

Copy link to clipboard

Copied

One can define a function in JavaScript. This function is like the function in mathematics in that you have parameters that provide the values to be processed or in this case the field names to be processed. If there is some type of systemic construct for naming the fields, it is possible to compute the names of the fields to be processed.

Votes

Translate

Translate

Report

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
New Here ,
Jan 24, 2018 Jan 24, 2018

Copy link to clipboard

Copied

So your answer to my very specific question about how to refer to a field by not using it's name is to be as vague and cryptic as possible? Thanks for your help. I totally understand what I need to change in my code now just from your comment /s

Votes

Translate

Translate

Report

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
LEGEND ,
Jan 24, 2018 Jan 24, 2018

Copy link to clipboard

Copied

I would start by making 2 fields. A drop down box named "Dropdown" and a text field named "Text". The drop down box would have the option "Commit selected value immediately" selected, The options for the field would have entries like " ", "Pass", "Fail". The field would have a "Format" using the "Custom" option and a key stroke script of:

var cName = event.targetName;

var aName = event.targetName.split(".");

var oText = this.getField("Text." + aName[1]);

if(event.willCommit == false)

{

oText.required = false;

oText.value = event.changeEx;

if(event.changeEx == "Fail")

{

oText.required = true;

} // end cangeEx "Fail";

} // end willCommit;

I would then select both fields and use the right mouse context menu option "Create Multiple Copies" and create 5 down and 1 across. Then exit the "Prepare Forms" mode and test the form. Note this approach will work for any number of fields more than one.

Votes

Translate

Translate

Report

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 ,
Jan 24, 2018 Jan 24, 2018

Copy link to clipboard

Copied

You can refer to the field using this code: event.target

Votes

Translate

Translate

Report

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 ,
Jan 24, 2018 Jan 24, 2018

Copy link to clipboard

Copied

LATEST

Here's an article on Dropdown/list field programming:

https://www.pdfscripting.com/public/List-Field-Usage-and-Handling.cfm

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

Votes

Translate

Translate

Report

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