Skip to main content
April 6, 2016
Answered

Checking if a string contains "text" from a box, and only if it doesn't, add it to the string.

  • April 6, 2016
  • 1 reply
  • 1303 views

I have a few comboboxes. I want a text field to add a string to itself, containing the text selected from each combobox. I got it, using this:

    var a = this.getField("CoBi_text");

    for (i=1;i<16;i++) {

        var t = (this.getField("CoB_"+i).valueAsString);

        if (t != ""){

            a.value += t+", ";   }   }

But this adds the same text if I select the same option in any combobox. How do I include a conditional do skip the increment if "text is already in the string?  I tried this:

        if ( (t != "") && (a.value.indexOf("t")<0) ) {

           a.value += t+", "; }

But it causes the string to add only the first text, not incrementing any more text after that.

PS. this text field will be hidden and later used on another condition (if it contains "specific text"), some other actions trigger.
So the ", " (comma) is probably not necessary, but I think it shouldn't be in the way of a proper indexOf search (right?)

Thanks.

This topic has been closed for replies.
Correct answer

ehrm, nevermind. I got it.

the "t" inside of indexOf should not have "s....

var a = this.getField("CoBi_text");

    for (i=1;i<16;i++) {

        var t = (this.getField("CoB_"+i).valueAsString);

        if ( (t != "") && (a.value.indexOf(t)<0) ) {

          a.value += t+", "; }

Now it adds "text, " but no more than once for each different text on each dropdown list.

There's still the issue of changing a selected item in a combobox, how do I remove the old text from the textbox... but I think some form of forced recalculation should suffice.

Thanks for taking the time to answer and for the link!

1 reply

Inspiring
April 6, 2016

Has a selection for all combo boxes been made before your script runs?

What is the default value for your combo boxes?

Have you double checked that the values you are trying to process are really what you think they are?

You could add some additional statements temporarily  to show the values and type of values you are processing using the console.println() method.

You might want to create an array variable to hold all the values you whish to process. You then have other options on how to create the final result. You can check for an aleady included value using the "indexOf() " method. before adding the value.

Unless you are adding the elements into a combo box or drop down box, the default value for the no selection is a space not a null string. If you set a default value you can compare the "value" of the combo box to the "defautlValue" of the combo box.

Since a combo box allows for more than one selection, how will h

andle those situations?

April 6, 2016

gkaiseril wrote:

Has a selection for all combo boxes been made before your script runs?

What do you mean? the comboboxes are named sequentially, "CoB1", "CoB2", ... , "CoB15". Then with the interval loop they are checked one by one. Is this incorrect? (it did work, just not with the condition I wanted of not copying the same text (each from export value of the comboboxes) into the textbox.

The values of each choice from the list of each combobox are equal to the text they show on the dropdown list.
So for example: the CoB1 has A, B, C, 1, 2, 3, !, @, # to be chosen from its dropdown list. When I choose B, "B, " is added to my textbox. When I choose again, this time @, "@, " is added to my textbox. The same goes for CoB2, etc. So if I choose an item from all the 15 combobox (each with 50+ items, not as brief as this example) the textbox is populated with the string "A, C, 2, !, A, B, D, Z, " etc...
What I'm trying to filter out is the A (in the aforementioned example string) from being repeated in the string, so If I choose "A" in 5 different checkboxes, "A, " is added only once to the textbox.

The use of indexOf() was exactly what I tried, as I mentioned, but didnt quite work. I was hoping for some instructions of whether my syntax mistakes or an alternative to this process as a whole.

I will read your links and try other things but any additional guidance is more than welcome, Thanks!

Inspiring
April 6, 2016

It is possible to set a combo box to allow the selection of more than one option from the list of options for the combo box.  If this option is enabled for a combo box, then one needs additional coding to detect this situation and obtain the data. currentValueIndices