I am trying to pass the value of all the textareas on a page to a JSON table. For some reason I get a "cannot set value of undefined error", I dont understand since my Json is defined earlier.
Here is the code with the 2 functions.
function parseToJson() {
var json = {};
$("input.invoice_details").each(function () {
json[this.id] = $(this).val();
});
$("select.invoice_details").each(function () {
json[this.id] = $(this).val();
});
json.categories = {};
$("textarea.invoice_categories").each(function () {
if (this.value.trim() !== '') {
json.categories[this.id].values = splitInput(this);
json.categories[this.id].label = this.name;
}
});
console.log(JSON.stringify(json));
generatePreveiw(json);
}
function splitInput(textArea) {
var input = textArea.value.trim();
var lines = input.split("\n");
var array = [];
$.each(lines, function (indexLine, line) {
var columns = line.split("\t");
var obj = {};
$.each(columns, function (indexColumn, column) {
var columnName = columnsName.columnsName[indexColumn].name;
obj[columnName] = column;
});
array.push(obj);
});
return array;
}
var json = {};— That isn't JSON. That is a JavaScript object. JSON is an external data format that happens to be inspired by JavaScript literal syntax.