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

Help in finding right script

Explorer ,
Aug 10, 2023 Aug 10, 2023

I got a script from chatGPT for adobe acrobat dc pro

 

"When I select a value from dropdown list and want on a textbox automatically like

When I select N/A then textbox should be empty

when I select 0 then textbox should have a value of 0%

when I select 1 then textbox should have a value of 25%

when I select 2 then textbox should have a value of 50%

when I select 3 then textbox should have a value of 75%

when I select 4 then textbox should have a value of 100%"

 

but I dont get any result when I run the followng javascript, 

 

// Replace "DropdownFieldName" and "TextboxFieldName" with the actual field names in your document
var dropdownField = "DropdownFieldName";
var textboxField = "TextboxFieldName";

// Define the mapping of dropdown values to textbox values
var valueMapping = {
"N/A": "",
"0": "0%",
"1": "25%",
"2": "50%",
"3": "75%",
"4": "100%"
};

// Get the dropdown and textbox fields
var dropdown = this.getField(dropdownField);
var textbox = this.getField(textboxField);

// Set up a change event handler for the dropdown field
dropdown.setAction("OnBlur", function() {
var selectedValue = dropdown.value;

// Set the textbox value based on the selected value from the dropdown
if (valueMapping.hasOwnProperty(selectedValue)) {
textbox.value = valueMapping[selectedValue];
}
});

TOPICS
Comment review and collaborate Experiment , Create PDFs , General troubleshooting , JavaScript , PDF , PDF forms
1.1K
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 ,
Aug 11, 2023 Aug 11, 2023
LATEST

If you format the text field as percentage, then set the export value like this:

In export value for 0 enter 0 for 1 give it export value 0.25...etc

 

If you didn't set format in text field, then set export values like this:

In export value for 0 enter 0% for 1 give it export value 25%...etc

 

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
Adobe Employee ,
Aug 10, 2023 Aug 10, 2023

Hi @Alizai,

 

Hope you are doing well. Thank you for writing in!

 

Instead, I prefer the If-Else statement to capture the values for drop-downs and fields. 

I tried a script that worked for me:

function onDropdownChange(event) {
  var dropdown = event.target;
  var textbox = document.getElementById("textbox");

  if (dropdown.value == "N/A") {
    textbox.value = "";
  } else if (dropdown.value == "0") {
    textbox.value = "0%";
  } else if (dropdown.value == "1") {
    textbox.value = "25%";
  } else if (dropdown.value == "2") {
    textbox.value = "50%";
  } else if (dropdown.value == "3") {
    textbox.value = "75%";
  } else if (dropdown.value == "4") {
    textbox.value = "100%";
  }
}

document.addEventListener("change", onDropdownChange);

 

Hope this helps.

 

Note: I am not an expert when it comes to scripting, but this is something that worked for me.

 

-Souvik

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
Explorer ,
Aug 10, 2023 Aug 10, 2023
Thanks for sending me a valuable script, Hats off,

I got an error when I run the script
i.e
"document is not defined"

would you please help me? thanks
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
Adobe Employee ,
Aug 10, 2023 Aug 10, 2023

@Alizai, You get the error because document is not defined as a variable in the above context.

 

Try adding this to the above code as the first line:

var document = window.document;

 

 

Hope this helps.

 

-Souvik

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 ,
Aug 10, 2023 Aug 10, 2023

The script is for web browsers, not Adobe Acrobat..

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
Explorer ,
Aug 11, 2023 Aug 11, 2023

Ok then please make a script for adobe acrobat pro, I would be grateful.

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
Enthusiast ,
Aug 11, 2023 Aug 11, 2023

@AlizaiYou don't need a complex script, take a look at Nesa's 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 Expert ,
Aug 10, 2023 Aug 10, 2023

Add percentage as 'export value' to dropdown choices, then use this as custom calculation script of dropdown field:

this.getField("textboxField").value = event.value;
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
Explorer ,
Aug 11, 2023 Aug 11, 2023

It works but not according to what I want, it gives what is inside dropdown list, i.e if "0" then it gives 0%, if "1" then gives 1% and so on, I need if dropdown value is "0", textbox value should be 0% and if dropdown value is "1", textbox value should be "25%" and so on. so please make such script or coding to give the required result. Thanks

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 ,
Aug 11, 2023 Aug 11, 2023
LATEST

If you format the text field as percentage, then set the export value like this:

In export value for 0 enter 0 for 1 give it export value 0.25...etc

 

If you didn't set format in text field, then set export values like this:

In export value for 0 enter 0% for 1 give it export value 25%...etc

 

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