Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session

Single or multiple check box values displayed in a textbox

Avatar

Level 4


Hi Experts!

I'm using the following script to display the values of check boxes in a text field.  I would like the word "and" to separate the values if two check boxes are selected. If more than two boxes are selected I want the values to be separated by a comma and then the last value be separated by "and".  Example of two check boxes selected:  A and B.  Example of two or more: A, B, C, and D.  Can this be done?

 

form1.page1.page1SF.programs::calculate - (JavaScript, client)

//displays the values of check boxes named programCB

var aChkBx = [];

var vChecks = programCB.all;

for (var a=0; a<vChecks.length;a++){

if(!vChecks.item(a).isNull) {

aChkBx.push(vChecks.item(a).rawValue);

}

}

this.rawValue = aChkBx.join(", ");

1 Reply

Avatar

Level 7

So, the way I see it, you actually have four scenarios. Nothing is checked, one item is checked, two items are checked, and three or more items are checked.

Here's how I did it.

I have four checkboxes all with the same name, cb. (You could have many more, and it wouldn't matter as long as they're all named the same thing.) I'm presuming that you will use the captions from the checkboxes as the text you want to enter. I called my text area where I enter the information tfSentence. (It's not a text field, it's just called "Text" in the Object Library.)


//we need to count how many boxes are checked


var checked = 0;



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


  if (xfa.resolveNode("cb["+i+"]").rawValue == 1) checked ++;


}



//create a string variable to store our sentence


var str = "";



//look at the checked variable and choose our case from that


switch(checked){


  case 0:


    //you may want to do more than just type out a sentence here, like a message box


    tfSentence.rawValue = "Nothing was selected";


    break;


  case 1:


    //you can set text that will appear before your list of items here


    //str = "preliminary text";


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


      if (xfa.resolveNode("cb["+i+"]").rawValue == 1) str += xfa.resolveNode("cb["+i+"].caption.value.#text").value;


    //you can set text to appear after your list of items here


    //str += "ending text";


    tfSentence.rawValue = str;


    break;


  case 2:


    //str = "preliminary text";


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


      if (xfa.resolveNode("cb["+i+"]").rawValue == 1){


        if (checked == 1) str += " and " + xfa.resolveNode("cb["+i+"].caption.value.#text").value;


        else str += xfa.resolveNode("cb[+i+"].caption.value.#text").value;


        checked--;


      }


    }


    //str+= "ending text";


    tfSentence.rawValue = str;


    break;


  default:


    //str = "preliminary text";


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


      if (xfa.resolveNode("cb["+i+"]").rawValue == 1){


        if (checked == 1) str += "and " + xfa.resolveNode("cb["+i+"].caption.value.#text").value;


        else str += xfa.resolveNode("cb["+i+"].caption.value.#text").value + ", ";


        checked--;


      }


    }


    //str += "ending text";


    tfSentence.rawValue = str;


    break


}