get checkbox values if it is true in client script

David Cross
Tera Expert

Hello all

 

I have more then 50 checkbox in my catalog items.. Among this 50 checkbox only the selected values must be shown in the multiline text. 

My below code is not working, I'm testing it with 3 checkbox in the below script.. Please help me here 

function onSubmit() {

  var v1 = g_form.getValue('check1');

var v2 = g_form.getValue('check2');

 var v3 = g_form.getValue('check3');

var arr = [v1, v2, v3];

    var arr2 = [];

    for (var i in arr) {

 

        if (arr[i] == true) {

            arr2.push(arr[i]);

            g_form.setValue('check_box_values', arr2);

 

        }

}

 }

 

1 ACCEPTED SOLUTION

Suyog Aptikar
Giga Guru

@David Cross  can you try  (arr[i] == 'true') i.e. true as string instead of Boolean once and see if that makes it work.

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.



Best regards

Suyog

View solution in original post

5 REPLIES 5

Suyog Aptikar
Giga Guru

@David Cross  can you try  (arr[i] == 'true') i.e. true as string instead of Boolean once and see if that makes it work.

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.



Best regards

Suyog

Thank You Suyog, This worked; 

Danish Bhairag2
Tera Sage
Tera Sage

Hi @David Cross ,

 

Can u try below code:

 

function onSubmit() {

    var v1 = g_form.getValue('check1');

    var v2 = g_form.getValue('check2');

    var v3 = g_form.getValue('check3');

 

    var arr = [];

    

    if (v1) {

        arr.push('Check 1 Label or Value'); // Replace 'Check 1 Label or Value' with the actual label or value of the checkbox

    }

    

    if (v2) {

        arr.push('Check 2 Label or Value'); // Replace 'Check 2 Label or Value' with the actual label or value of the checkbox

    }

    

    if (v3) {

        arr.push('Check 3 Label or Value'); // Replace 'Check 3 Label or Value' with the actual label or value of the checkbox

    }

 

    var multilineText = arr.join('\n'); // Join the array elements with newline characters

    

    g_form.setValue('check_box_value

s', multilineText);

}

 

Thanks,

Danish

 

Vishal Birajdar
Giga Sage

Hi @David Cross 

 

Try like below :

function onSubmit() {

var v1 = g_form.getValue('check1');
var v2 = g_form.getValue('check2');
var v3 = g_form.getValue('check3');

var arr   = [v1, v2, v3];
var arr2 = [];

    for (var i in arr) {

        if (arr[i] == true) {
            arr2.push(arr[i]);

        }

}


            g_form.setValue('check_box_values', arr2.toString());   //set the value after for loop


 }

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates