Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

How to loop through checkboxes and create a comma separated string with the values

heathers_
Kilo Sage

Hello. We have a catalog item for users to request access using checkbox variables. The user can select one checkbox or multiple. 

 

The requirement is to send the label/name of the selected checkboxes in a comma separated string in an existing REST API.

 

My challenge is around arrays. How do I loop through the checkboxes that were selected to create a single comma separated list?

Example 1: Both checkboxes were selected, the string value should be "AuditBoard,BOX"

heathers__0-1724166902604.png

Example 2: One checkbox was selected, the string value should be "BOX"

heathers__1-1724167021322.png

Thank you for your help.

1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron

@heathers_ You can try the following onLoad script and form an array of selected checkbox label.

Let's say you have the following checkbox fields:

  1. u_checkbox_1
  2. u_checkbox_2
  3. u_checkbox_3

 

 

function onSubmit() {
    var selectedValues = [];  // Array to store selected checkbox values

    // Check each checkbox field and add its value to the array if it is checked
    if (g_form.getValue('u_checkbox_1') == 'true') {
        selectedValues.push(g_form.getLabelOf('u_checkbox_1'));  // Push the label or value
    }
    if (g_form.getValue('u_checkbox_2') == 'true') {
        selectedValues.push(g_form.getLabelOf('u_checkbox_2'));
    }
    if (g_form.getValue('u_checkbox_3') == 'true') {
        selectedValues.push(g_form.getLabelOf('u_checkbox_3'));
    }

    // Join the array into a single comma-separated string
    var selectedValuesString = selectedValues.join(', ');

    // Set the value of the target field with the comma-separated list
    alert(selectedValuesString);

    return true;  // Allow the form submission to proceed
}

 

 

View solution in original post

2 REPLIES 2

Sandeep Rajput
Tera Patron

@heathers_ You can try the following onLoad script and form an array of selected checkbox label.

Let's say you have the following checkbox fields:

  1. u_checkbox_1
  2. u_checkbox_2
  3. u_checkbox_3

 

 

function onSubmit() {
    var selectedValues = [];  // Array to store selected checkbox values

    // Check each checkbox field and add its value to the array if it is checked
    if (g_form.getValue('u_checkbox_1') == 'true') {
        selectedValues.push(g_form.getLabelOf('u_checkbox_1'));  // Push the label or value
    }
    if (g_form.getValue('u_checkbox_2') == 'true') {
        selectedValues.push(g_form.getLabelOf('u_checkbox_2'));
    }
    if (g_form.getValue('u_checkbox_3') == 'true') {
        selectedValues.push(g_form.getLabelOf('u_checkbox_3'));
    }

    // Join the array into a single comma-separated string
    var selectedValuesString = selectedValues.join(', ');

    // Set the value of the target field with the comma-separated list
    alert(selectedValuesString);

    return true;  // Allow the form submission to proceed
}

 

 

Not applicable