I have a function that takes in, for example, 10 textboxes worth of values and puts them into a JSON string that I then store in a cookie. I have no issues if I hard code the problem where I'm grabbing the element "assignment[]", but I'd also like to add other text box values to it, say "quizzes[]", as an example, in order to have one long string that I would then convert to a JSON string.
function setObject(name, score)
{
this.name = name;
this.score = score;
}
function setCookie()
{
var cookieName = "assignments";
var cookieValue = document.getElementsByName("assignments[]");
var arr = [];
for(var i=0;i<cookieValue.length;i++)
{
var setObj = new setObject(cookieName + i, cookieValue[i].value);
arr.push(setObj);
}
document.cookie = JSON.stringify(arr);
}
This code above works just fine for just the "name[]" textboxes, but I'd like to be able to add other elements to that same JSON string.
My current output would look like this:
[{"name":"assignments0","score":"1"},{"name":"assignments1","score":"2"},
{"name":"assignments2","score":"3"},{"name":"assignments3","score":"4"}]
My expected output would look like this if I were able to add different textbox arrays through my function:
[{"name":"assignments0","score":"22"},{"name":"assignments1","score":"19"},
{"name":"assignments2","score":"9"},{"name":"assignments3","score":"20"},
{"name":"quizzes0","score":"5"},{"name":"quizzes1","score":"9"}]
Any help in the right direction would be much appreciated.