JSFiddle - React, Tailwind, and code Playground
HTML
<input type="text" name="selected_stores" id="selected-stores" value='{"selected_stores":[{"row_0":"3366","row_1":"3366"}]}'>
<br/>
<input type="checkbox" class="chk-select" value="4353"/>
<br/>
<input type="checkbox" class="chk-select" value="5145"/>
<br/>
<input type="checkbox" class="chk-select" value="8778"/>
<br/>
<input type="checkbox" class="chk-select" value="4547"/>
<br/>
<input type="button" id="store-save" value="save" data-row="2"/>
JavaScript
jQuery("#store-save").click(function() {
var checkedVal, outcheckedStr, temp, row_num, selected_stores, selected_row_data, param, selected_stores_jsonObjStr, checkedStr = '';
var selected_stores_jsonObj;
selected_stores = jQuery("#selected-stores").val();
jQuery('.chk-select:checked').each(function() {
checkedVal = jQuery(this).val();
temp = checkedVal + '::';
checkedStr += checkedVal + '::';
});
outcheckedStr = checkedStr.replace(/(^\s*::)|(::\s*$)/g, '');
row_num = jQuery("#store-save").attr('data-row');
selected_row_data = '{"row_' + row_num + '":"' + outcheckedStr + '"}';
if (selected_stores === "") {
selected_stores_json = '{"selected_stores":' + selected_row_data + '}';
} else {
selected_stores_jsonObj = jQuery.parseJSON(selected_stores);
selected_stores_jsonObj.selected_stores['row_' + row_num] = outcheckedStr;
console.log(JSON.stringify(selected_stores_jsonObj));
alert(JSON.stringify(selected_stores_jsonObj));
selected_stores_jsonObjStr = JSON.stringify(selected_stores_jsonObj);
selected_stores_json = selected_stores_jsonObjStr;
//alert(selected_stores_json);
}
jQuery("#selected-stores").val(selected_stores_json);
});