How to refer to dropdown not by name

New Here ,
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.

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

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

TOPICS
Acrobat SDK and JavaScript, Windows

Views

89

Likes

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

How to refer to dropdown not by name

New Here ,
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.

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

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

TOPICS
Acrobat SDK and JavaScript, Windows

Views

90

Likes

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
Jan 24, 2018 0
Adobe Community Professional ,
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.

Likes

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
Reply
Loading...
Jan 24, 2018 0
New Here ,
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

Likes

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
Reply
Loading...
Jan 24, 2018 0
Adobe Community Professional ,
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.

Likes

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
Reply
Loading...
Jan 24, 2018 0
Most Valuable Participant ,
Jan 24, 2018

Copy link to clipboard

Copied

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

Likes

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
Reply
Loading...
Jan 24, 2018 1
Adobe Community Professional ,
Jan 24, 2018

Copy link to clipboard

Copied

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

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

Likes

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
Reply
Loading...
Jan 24, 2018 0