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

Dropdown box fill with dashes in blank space after entering value.

Explorer ,
May 03, 2024 May 03, 2024

Copy link to clipboard

Copied

I have a dropdown list in acrobat form. I want to achive this that after selection of item from dropdown list it will calculate the blank space available in the dropdown box and fill it with "------------------". 

 

The number of "dashes" required to fill the box will change depending on the item selection from the dropdown list. Thanks in advance. Please Help!!!

TOPICS
JavaScript , PDF , PDF forms

Views

263

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 , May 03, 2024 May 03, 2024

Use this code:

 

if (event.value!=event.target.defaultValue) {
	var selectedText = event.value;
	var remainingSpace = 30 - selectedText.length;
	var dashes = "-".repeat(remainingSpace);
	var filledText = selectedText + dashes;
	event.value = filledText;
}

Votes

Translate

Translate
Community Expert ,
May 03, 2024 May 03, 2024

Copy link to clipboard

Copied

Why do you need to calculate it? The values of the drop-down box are fixed, aren't they? Just count how many dashes fit for each value and then hard-code it in your script. Calculating the length of a random string in a (non-monospaced) font is extremely difficult.

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
Explorer ,
May 03, 2024 May 03, 2024

Copy link to clipboard

Copied

I apolozize for my shortcommings. I am unable to code it in the way you are instructing.

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 ,
May 03, 2024 May 03, 2024

Copy link to clipboard

Copied

All you need to do is the following:

if (event.value=="Option 1") event.value = "Option 1---------";

else if (event.value=="Option 2") event.value = "Option 2-----------";

else if (event.value=="Option 3") event.value = "Option 3---------------";

etc.

And use it as the field's custom Format script.

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
Explorer ,
May 03, 2024 May 03, 2024

Copy link to clipboard

Copied

Is it possible to get a solution by this type of script? Because i have more than 100 items in the dropdown. 

var selectedText = event.value;
var maxLength = event.target.fieldFullLength;
var remainingSpace = maxLength - selectedText.length;
var dashes = "-".repeat(remainingSpace);
var filledText = selectedText + dashes;
event.value = filledText;

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 ,
May 03, 2024 May 03, 2024

Copy link to clipboard

Copied

- There's no such property as "fieldFullLength".

- The repeat method will not work in older viewers.

- There's no easy way to calculate the length of a string of non-monospaced fonts.

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
Explorer ,
May 03, 2024 May 03, 2024

Copy link to clipboard

Copied

If i use monospaced fonts then also it would not be possible right?

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 ,
May 03, 2024 May 03, 2024

Copy link to clipboard

Copied

It would be much easier... You will just need to know how many characters fit in the area of the drop-down field, and then deduct the length of each string from that value, and fill in the rest with dashes. Basically what your original code tried to do.

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
Explorer ,
May 03, 2024 May 03, 2024

Copy link to clipboard

Copied

like this?

var selectedText = event.value;
var remainingSpace = 30 - selectedText.length;
var dashes = "-".repeat(remainingSpace);
var filledText = selectedText + dashes;
event.value = filledText;

 Then place it in the dropdowns validation script? 

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 ,
May 03, 2024 May 03, 2024

Copy link to clipboard

Copied

Yes, although I would use the Format event, so it doesn't actually change the field's value.

Also, as I wrote earlier, the repeat method will not work on older versions of Acrobat and Reader. Just FYI.

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
Explorer ,
May 03, 2024 May 03, 2024

Copy link to clipboard

Copied

Almost to my goal. This code works fine in Custom format option. 

var selectedText = event.value;
var remainingSpace = 30 - selectedText.length;
var dashes = "-".repeat(remainingSpace);
var filledText = selectedText + dashes;
event.value = filledText;

But it fills all the blank dropdown when no data is in dropdown. your help is needed to achieve this so that only when there will data only then there will be "------- "up to the whole dropdown leangth.

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 ,
May 03, 2024 May 03, 2024

Copy link to clipboard

Copied

By "blank", do you mean when the default value is selected?

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
Explorer ,
May 03, 2024 May 03, 2024

Copy link to clipboard

Copied

Yes. Default value is blank.

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 ,
May 03, 2024 May 03, 2024

Copy link to clipboard

Copied

Use this code:

 

if (event.value!=event.target.defaultValue) {
	var selectedText = event.value;
	var remainingSpace = 30 - selectedText.length;
	var dashes = "-".repeat(remainingSpace);
	var filledText = selectedText + dashes;
	event.value = filledText;
}

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
Explorer ,
May 04, 2024 May 04, 2024

Copy link to clipboard

Copied

LATEST

Finally it worked. U are really the generous king of the Acrobat JavaScript API. I cant thank you!!!

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