I have an html like below
HTML:
<INPUT TYPE=CHECKBOX NAME="clcik" onClick="add('1234','blah')" />
<input type="hidden" id="project" value="" />
JS:
function add(obj1 , obj2){
var jsonArray = [];
var jsonObj = { product_id : obj1 , name : obj2 };
jsonArray.push(jsonObj);
var field = document.getElementById('project');
field.setAttribute('value', JSON.stringify(jsonArray));
alert(field.getAttribute('value'));
}
I am trying to set first and retrieve again to check but nothing is happening..I can’t understand where I am wrong…?
I guess you missed to get the stringify result into
stringDatavariable because of which you are getting a js error before it reaches the line where you are trying to alert the value.JSONorJSON.stringifyis not provided byjQueryyou have to include json2 library on the page if the browser natively do not support it.Try this
Code to remove element from array based on your request in the comment.