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

Merging text fields based on checkbox

Community Beginner ,
Apr 06, 2021 Apr 06, 2021

Copy link to clipboard

Copied

I have 10 checkboxes next to 10 text fields (ie cb1 next to t1, cb2 next to t2,...cb10 next to t10). 

Below all of this is a text field "tmerge".  Is there a way that say if cb4 adn cb8 are selected then the text fields strings from the neighbouring text fields t4 and t8 are merged into the tmerge text field?

 

TOPICS
How to , JavaScript

Views

2.4K

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 , Apr 06, 2021 Apr 06, 2021

Try this as calculation script of "tmerge" field:

event.value = "";
for ( var i=1; i<=10; i++){
if(this.getField("cb"+i).valueAsString != "Off")
event.value += this.getField("t"+i).valueAsString+" ";}

Votes

Translate

Translate
Community Expert ,
Apr 06, 2021 Apr 06, 2021

Copy link to clipboard

Copied

Try this as calculation script of "tmerge" field:

event.value = "";
for ( var i=1; i<=10; i++){
if(this.getField("cb"+i).valueAsString != "Off")
event.value += this.getField("t"+i).valueAsString+" ";}

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 Beginner ,
Apr 06, 2021 Apr 06, 2021

Copy link to clipboard

Copied

Hi Nesa, many thanks that works well.  Could you also tell me what script I use to add a pre or post text string to the merged text fields?  For example, the final merged script will look something like: "These are the text scripts:" t1 t2 t3; or t1 t2 t3 "these are the text scripts." ?

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 ,
Apr 07, 2021 Apr 07, 2021

Copy link to clipboard

Copied

Use this and remove pre or post text if not needed:

var txt = "";
for ( var i=1; i<=10; i++){
if(this.getField("cb"+i).valueAsString != "Off")
txt += this.getField("t"+i).valueAsString+" ";
if(txt == "")
event.value = "";
else
event.value = "pre text goes here "+txt+" post text goes here";}

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 Beginner ,
Apr 07, 2021 Apr 07, 2021

Copy link to clipboard

Copied

Beautiful.  Many thanks.  One final adaptation if you don't mind.  How could this script be adapted to allow cb1 to cb5 (and therefore t1 to t5) with a pre text while cb6 to cb10 (t6 to t10) with a post text string?  I expect it could be done by repeating the same script and changing cb5 to cb10 to something else like cbx1 to cbx5 however I was hoping to keep the same cb and t names in order to more easily group and hide/show them with a button. 

 

I tried repeating the script and changing the i = 6, but then it only recognises cb6 to cb10.  If I could somehow separate the repeated scripts (if else ?) then perhaps that may work.

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 ,
Apr 07, 2021 Apr 07, 2021

Copy link to clipboard

Copied

You want both to be shown in same text field?

If cb1-cb5 there is pre text but if one of cb6-cb10 checked you want also post text?

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 Beginner ,
Apr 07, 2021 Apr 07, 2021

Copy link to clipboard

Copied

After re-thinking it I agree with what your probably alluding to that they should be in separate merged text fields.  This I have done and it works very well using your above script. 

 

What would be nice is if I multi-select say cb1, cb3, cb5 that they are coma separated.  Looking something like: t1, t3, t5 "post text goes here".  Is that feasible?

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 ,
Apr 08, 2021 Apr 08, 2021

Copy link to clipboard

Copied

In this line:

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

just change " " to ","

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 Beginner ,
Apr 08, 2021 Apr 08, 2021

Copy link to clipboard

Copied

Is there a way of modifying the below script that works perfectly for merging an associated checkbox and text field to an associated radiobutton and text field.  I tried replacing the cb with a rb (radiobutton) without success.

 

var txt = "";
for ( var i=1; i<=10; i++){
if(this.getField("cb"+i).valueAsString != "Off")
txt += this.getField("t"+i).valueAsString+" ";
if(txt == "")
event.value = "";
else
event.value = "pre text goes here "+txt+" post text goes here";}

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 ,
Apr 08, 2021 Apr 08, 2021

Copy link to clipboard

Copied

Lets say your radio buttons are named by default "Group1" and you didn't change default rb choice (Choice1-Choice10) you can use this script:

var rf = "";
var txt = "";
if(this.getField("Group1").valueAsString != "Off"){
rf = this.getField("Group1").valueAsString.split("Choice");
txt = this.getField("t"+rf[1]).valueAsString;}
if(txt == "")
event.value = "";
else
event.value = "pre text goes here "+txt+" post text goes here";

 

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 Beginner ,
Apr 09, 2021 Apr 09, 2021

Copy link to clipboard

Copied

Nesa, you're a Javascript rockstar.  Many thanks.

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 Beginner ,
Apr 15, 2021 Apr 15, 2021

Copy link to clipboard

Copied

Hi Nesa,

 

I am using the below JS you kindly provided to obtain a radio button selection that adds some pre or post script into another Text Field.  What I am trying to do now is combine (merge) the radio button selection from multiple different groups of radio buttons into the same Text Field.  Is there a way I can repeat the same JS in the Text Field calculation JS to "pull" the different radio button groups in a particular order?  Or is the only way to place the radio button selections into separate Text Fields and then merge them together into a "final" Text Field merge?  Is that clear as mud?

 

var rf = "";
var txt = "";
if(this.getField("Group1").valueAsString != "Off"){
rf = this.getField("Group1").valueAsString.split("Choice");
txt = this.getField("t"+rf[1]).valueAsString;}
if(txt == "")
event.value = "";
else
event.value = "pre text goes here "+txt+" post text goes here";

 

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 ,
Apr 15, 2021 Apr 15, 2021

Copy link to clipboard

Copied

some basic script would be:

var rf = "";
var rf2 = "";
var txt = "";
var txt2 = "";
if(this.getField("Group1").valueAsString != "Off" && this.getField("Group2").valueAsString != "Off"){
rf = this.getField("Group1").valueAsString.split("Choice");
rf2 = this.getField("Group2").valueAsString.split("Choice");
txt = this.getField("t"+rf[1]).valueAsString;
txt2 = this.getField("x"+rf2[1]).valueAsString;}
if(txt == "")
event.value = "";
else
event.value = "pre text goes here "+txt+" "+txt2+" post text goes here";

 

But from here it all depends how you want it to work.

Above code will show text only when choice is made in both groups.

 

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 Beginner ,
Apr 15, 2021 Apr 15, 2021

Copy link to clipboard

Copied

Thanks Nesa for your prompt help.  I will have a play with this and see if I can adapt it to my needs.  Great learning experience along the way. 

 

One further question, can you suggest a method whereby the final merged text field can be changed/adjusted either within the merged field directly or indirectly by shifting the merged script into another text field.  I assume the potential JS method involves the script being "pushed" into the modifable text field rather than being "pulled" into the field.  Such that, if the initial Radio Button selection is changed then this will alter the initial merged text.  What I was hoping to achieve is a merged text field from a bunch of Radio Button groups that can be altered at the end.  Thank again for your help.

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 ,
Apr 15, 2021 Apr 15, 2021

Copy link to clipboard

Copied

Yes, you could push text to another field using validation in first field, then you could change text in second field as long as you don't change anything in first field after that, or you could stop calculation in first field, for example using checkbox, then you can change text in first field whitout need for second field.

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 Beginner ,
Apr 16, 2021 Apr 16, 2021

Copy link to clipboard

Copied

Hi Nesa, your first method I expect would work best.  I'd like to have the ability to be able to go back and change the initial radio button selection which would then reset the merged text field.  From here I could alter the final merged text.  Could you please educate me on the validation method in the first field?

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 ,
Apr 16, 2021 Apr 16, 2021

Copy link to clipboard

Copied

As validation script of first field use this (change field name):

this.getField("second field name").value = event.value;

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 Beginner ,
Apr 16, 2021 Apr 16, 2021

Copy link to clipboard

Copied

Not quite there.  So I have 6 List Boxes merging into one Text Field (tf1).  Within tf1 (Validate tab) I place:

this.getField("tf2").value = event.value;

 

tf2 is using the below JS to copy the script from tf1:

event.value = this.getField("tf1").value

 

Where am going wrong Nesa?

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 ,
Apr 16, 2021 Apr 16, 2021

Copy link to clipboard

Copied

Do you get any errors or it doesn't work?

You don't need script in "tf2" field.

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 Beginner ,
Apr 16, 2021 Apr 16, 2021

Copy link to clipboard

Copied

Sorry Nesa, you were right (as usual).  I wasn't aware that the script you provided was indeed pushing into the tf2 field.  So yes no script was required in it and yes it worked excellently.  Once again, many thanks.

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 Beginner ,
Apr 16, 2021 Apr 16, 2021

Copy link to clipboard

Copied

Hi Nesa, is there a way I can have double or triple spaces bewteen text (because some merging list items were not selected) reduced down to a single space before or as the text is "pushed" to "tf2"?

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 Beginner ,
Apr 09, 2021 Apr 09, 2021

Copy link to clipboard

Copied

With respect to the coma separating the t scripts, the suggested method provides a coma between each t value as well as at the end.  Is there a way of placing a coma between each and then an "and" between the last two t scripts and nothing after the last t 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 ,
Apr 09, 2021 Apr 09, 2021

Copy link to clipboard

Copied

Can you post what exactly script you are using?

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 Beginner ,
Apr 09, 2021 Apr 09, 2021

Copy link to clipboard

Copied

The checkbox list has a title: Sports Played.  The list and associated text field is of common sports.  The merged output currently looks like:

Sports Played: football baseball soccer tennis

or

Sports Played: football, baseball, soccer, tennis,

 

The preferred output would be:

Sports Played: football, baseball, soccer and tennis

 

The actual list is up to 20 sports.

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 ,
Apr 09, 2021 Apr 09, 2021

Copy link to clipboard

Copied

What i meant was, what script do you use, one with checkboxes or radio buttons.

So there is 20 fields?

See if this works, it's the code for checkboxes 'cb1-cb20' and text fields 't1-t20' ( replace 20 if you have less fields):

var txt = "";
for ( var i=1; i<=20; i++){
if(this.getField("cb"+i).valueAsString != "Off")
txt += this.getField("t"+i).valueAsString+",";}
var str = txt;
var r = str.replace(/,([^,]*)$/, ' $1');
var res = r.replace(/,([^,]*)$/, ' and $1');
if(txt == "")
event.value = "";
else
event.value = "Sports Played: "+res;

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