I have an input field like the one below
<input type="hidden" value="" id="inputField">
Now I have list of products and for each product I have a checkbox. When a user clicks on the checkbox, I get the product id and name. Now I want to save it again in the hidden field like below
<input type="hidden"
value="[{"product_id":123,"name":"stack"},{"product_id":456,"name":"overflow"}]"
id="inputField"
>
My first question is how I can do this and how can I create the JSON?
Secondly if the user again unchecks a product checkbox then I need to get the current hidden value and convert it into some data structure, remove the unchecked box id from the data structure and then save it again in the hidden field.
Is there any library which does this job in JavaScript?