Via Cà Matta 2 - Peschiera Borromeo (MI)
+39 02 00704272
info@synaptica.info

List of all selected checkboxes into a JSON string

Digital Innovation Partner

List of all selected checkboxes into a JSON string

Add class to checkbox


Value 1
Value 2
Value 3
Value 4
Value 5

Create a function to return a JSON string


function arrayChk(){ 

    var arrAn = [];  
  
    var m = $('.myChk'); 

    var arrLen = $('.myChk').length; 
      
    for ( var i= 0; i < arrLen ; i++){  
        var  w = m[i];                     
         if (w.checked){  
          arrAn.push( { "myVal" : w.value } );  
          console.log(w.value ); 
        }  
      }   
    
    var myJsonString = JSON.stringify(arrAn);  //convert javascript array to JSON string
    console.log( 'result' + myJsonString);
    
    alert(myJsonString);

    return myJsonString;
  
   }

Here an example

https://jsfiddle.net/a3owjh85/1/