Skip to main content
Known Participant
October 24, 2022
Answered

Text Box with Export Values from Multiple Selections from a Dropdown Box with '\n' after each entry

  • October 24, 2022
  • 1 reply
  • 3159 views

Hi everyone.

First off, I am referring the original post...

Trying To Populate a Text Box with Export Values from Multiple Selections from a Dropdown List Box 

 

The document level function is being used here....

 

function fillRoles(doc, fieldName, newValue)
{
    var ar = [];
    // Get the value(s) currently stored in the dropdown value and 
    // convert them to an array
    var v = doc.getField(fieldName).value;
    if (v.length > 0) {
        var ar = v.split(",");
    }
    // Do we already have newValue in the array? 
    var foundIt = -1;
    for (var i=0; i<ar.length; i++) {
        if (ar[i] == newValue) {
            foundIt = i;
            break;
        }
    }
    if (foundIt != -1) {
        // remove the item
        ar.splice(i, 1);
    }
    else { // add the item at the end
        ar.push(newValue);
    }
    // convert the array to a string
    this.getField(fieldName).value = ar.join();
}

 

Custom Keystroke script on the Dropdown-

if(!event.willCommit) fillRoles(this, "textbox name here", event.changeEx);

The code works fine with "," separated export value  entry from dropdown list to the text field. Also work fine with the splice () function to remove the export value from the text field when the item is 2nd time clicked.

 

I have inserted a new line '\n' for each export value to be inserted with new line. 

 

this.getField(fieldName).value = ar.join('\n');

 

This creates a new line in each entry in the text field. But when i 2nd time click the same item from the dropdown list it does not remove the export value from the text field.

 

So this is what I am trying to achieve.

Is this possible? If so, does anyone have any idea of how to do something like this?

Thank you.

This topic has been closed for replies.
Correct answer Nesa Nurani

Sorry for the delay. The function was added to the document javascript as your advise. Thanks Again


Here you go: https://drive.google.com/file/d/1o6Dd3H1NL2db9bi91z5tvTLE1T1OH4Mr/view?usp=sharing 

1 reply

Nesa Nurani
Community Expert
Community Expert
October 24, 2022

If you just want to show each new value in new line you can use this as custom format script of text field:

var str = event.value;
if(event.value)
event.value = str.replace(",","\n");

elendil84Author
Known Participant
October 24, 2022

this.getField(fieldName).value = ar.join('\n');

this code works fine with each entry to be inserted with new line in the text field. But problem is the replacing the entered string when it is being clickd for the  2nd time.

Nesa Nurani
Community Expert
Community Expert
October 24, 2022

Yes, if you left your old script and used mine as custom format script it would show like this:

value1

value2

value3

But it would still leave value as value1,value2,value3.

But if you don't want just format, then change your script like this:

Change this part:

if (v.length > 0) {
var ar = v.split(",");
}

 

Change to this:

if (v.length > 0) {

var s = v.split("\r").join(",");

ar = s.split(",");
}