Currently delving into learning JSON as required for work purposes... I am attempting to dynamically add and delete records to a JSON object... Can anyone shed some light on why I keep receiving an UNDEFINED message? Here is the code... Appreciate any assistance!
<html>
<head>
<title>TEST</title>
<script language="Javascript" type="text/javascript">
function addC(){
var index = document.getElementById('type').selectedIndex;
var type = document.getElementById('type').options[index].value;
var name = document.getElementById('inpName').value;
var date = document.getElementById('inpDate').value;
processJson(type, name, date);
return false;
}
function processJson(type, name, date){
var count = document.getElementById('counter').value * 1;
var currentRecords = document.getElementById('holder').value;
var newRecordType = "{\"name\":\"" + type + "\",";
var newRecordName = "\"type\":\"" + name + "\",";
var newRecordDate = "\"date\":\"" + date + "\"}";
var newRecord = newRecordType + newRecordName + newRecordDate;
if (count > 0){
newRecord = "," + newRecord;
}
var updatedRecord = currentRecords + newRecord;
var jsonObj = {"allrows" : "[" + updatedRecord + "]"};
document.getElementById('counter').value = (document.getElementById('counter').value * 1) + 1;
document.getElementById('holder').value = updatedRecord;
}
function deleteRow(){
var toDel = document.getElementById('inpDel').value;
alert(toDel);
var current = "[" + document.getElementById('holder').value + "]";
alert(current);
var jsonO = {"allRows" : current};
alert(jsonO);
var t = jsonO.allRows[toDel].type;
alert("Deleting - " + t);
return false;
}
</script>
</head>
<body>
<form name="frm" action="">
<table>
<tr>
<td>
<select name="type" id="type">
<option value="creator">Creator</option>
<option value="editor">Editor</option>
<option value="publisher">Publisher</option>
</select>
</td>
<td>
<input type="text" name="inpName" id="inpName" value="">
</td>
<td>
<input type="text" name="inpDate" id="inpDate" value="">
</td>
<td>
<input type="text" name="inpDel" id="inpDel" value="">
</td>
<td>
<input type="button" name="cmdAdd" value="Add" onClick="return addC();">
<input type="button" name="cmdAdd" value="Del" onClick="return deleteRow();">
</td>
</tr>
<tr>
<td><input type="text" name="counter" id="counter" value="0">
</tr>
<tr>
<td colspan="3">
<textarea name="holder" id="holder" rows="20" cols="60"></textarea>
</td>
</tr>
</form>
</body>
</html>