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.

View solution in original post

5 REPLIES 5

Harsh_Deep
Giga Sage

Hello @David Cross 

 

Try this -

var i =0;
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 (i=0; i<arr.length; i++) {
        if (arr[i] == 'true') {
            arr2.push(arr[i]);
        }
}
g_form.setValue('check_box_values', arr2);
 }

Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.