List of all selected checkboxes into a JSON string
Add class to checkbox
1 2 3 4 5 6 7 |
<div id="checkboxList"> <div><input type="checkbox" value="1" class="myChk"> Value 1</div> <div><input type="checkbox" value="2" class="myChk"> Value 2</div> <div><input type="checkbox" value="3" class="myChk"> Value 3</div> <div><input type="checkbox" value="4" class="myChk"> Value 4</div> <div><input type="checkbox" value="5" class="myChk"> Value 5</div> <div> |
Create a function to return a JSON string
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
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