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

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

Explorer ,
May 03, 2024 May 03, 2024

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
1.2K
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 ,
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;
}

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

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.

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

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

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

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.

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

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

- 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.

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

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

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

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.

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

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? 

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

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.

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

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.

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

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

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

Yes. Default value is blank.

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 ,
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;
}
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 ,
May 04, 2024 May 04, 2024
LATEST

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

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