Skip to main content
Inspiring
March 6, 2017
Answered

How can I make check boxes generate paragraphs of text

  • March 6, 2017
  • 2 replies
  • 1639 views

Hi,

How can i make check boxes generate text?

I have 9 check boxes, each with their own individual Export Value. (The Export Value is a paragraph of text.)

I would like it so when people choose the check boxes they need, the result is shown in a text field, so I'm trying to write a custom calculation script.

My Checkboxes are called "Check Box 1" "Check Box 2" etc, and the results box is called "Strength results".

I can get it so I get all the text in one line, and the the word "Off" for each checkbox that isn't checked however I'm wanting:

Check Box 1 text (or which ever is selected)

Line break \n

Check Box 2 text (or which ever is selected)

Line break \n

Check Box 3 text (or which ever is selected)

Can you help?

Thanks

This topic has been closed for replies.
Correct answer Joel Geraci

Rename your checkboxes to "CheckBox.1", CheckBox.2" etc. then use the following code as your calculation in the field where you want the paragraphs to appear. "\n\n" will put an additional line break between the paragraphs.

var paras = [];

var fields = this.getField( "CheckBox" ).getArray();

// sort by name

fields.sort( function( a, b ) {

     var nameA = a.name.toUpperCase(); // ignore upper and lowercase

     var nameB = b.name.toUpperCase(); // ignore upper and lowercase

     if ( nameA < nameB ) {

          return -1;

     }

     if ( nameA > nameB ) {

          return 1;

     }

     // names must be equal

     return 0;

} );

for ( var i = 0; i < fields.length; i++ ) {

     var field = fields[ i ];

     if ( field.isBoxChecked( 0 ) ) {

          paras.push( field.value )

     }

}

event.value = paras.join( "\n\n " )

2 replies

Inspiring
March 7, 2017

Another question if anyone knows the answer (not as pressing as the other, hence not creating another thread).

We can set the Export Value of check box to be text easily within the properties menu, however, how can you insert a line break in a body of text in the export value? So you can export multiple paragraphs, as opposed to one...Any ideas?

Thanks

try67
Community Expert
Community Expert
March 7, 2017

You can only do that using a script, but it's not recommended. If you really want to do it I would recommend replacing the line-breaks with some kind of place-holder character, like the pipe symbol ("|"), and then you'll just need to replace it back when applying the value to your multiline text field.

Joel Geraci
Community Expert
Joel GeraciCommunity ExpertCorrect answer
Community Expert
March 6, 2017

Rename your checkboxes to "CheckBox.1", CheckBox.2" etc. then use the following code as your calculation in the field where you want the paragraphs to appear. "\n\n" will put an additional line break between the paragraphs.

var paras = [];

var fields = this.getField( "CheckBox" ).getArray();

// sort by name

fields.sort( function( a, b ) {

     var nameA = a.name.toUpperCase(); // ignore upper and lowercase

     var nameB = b.name.toUpperCase(); // ignore upper and lowercase

     if ( nameA < nameB ) {

          return -1;

     }

     if ( nameA > nameB ) {

          return 1;

     }

     // names must be equal

     return 0;

} );

for ( var i = 0; i < fields.length; i++ ) {

     var field = fields[ i ];

     if ( field.isBoxChecked( 0 ) ) {

          paras.push( field.value )

     }

}

event.value = paras.join( "\n\n " )

Inspiring
March 6, 2017

Amazing!! this is perfect.

Thank you so much Joel