Hi there, trying to learn json due to work req...i am trying to add and delete records to json object dynamically...can some one tell me why i keep getting UNDEFINED message....here is the code..thanks for the help!!
<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>