My issue is very simple.
I am trying to delete elements from a json array.
I want to know when the last element is deleted in order to hide a div
. So I do a check to find if jsonArray.Length > 0
.
The check is failing. The jsonArray.Length
is not getting reset. It always keeps the number of elements added into it even after deleting all elements. For eg: if 4 elements are added to it, then the Length remains as 4.
I will post the code below.
Initializers :
var ccount = 1;
var tasks = []
Inserting element into JSON array :
function insertTask() {
var out = '<div id="' + ccount + '" ><span style=" width:40px; display: inline-block; vertical-align: top; text-align:left;"><img src="../Images/iconrequestreport1.png" /></span><span style="width:160px; display: inline-block; vertical-align: top; text-align: left; font-family:Arial; font-size:small;"><div>' +
$('#drpTrajectory').find(":selected").text() + '</div><div>Start Time: ' +
$('#txtStartTime').val() + '</div><div>End Time: ' +
$('#txtEndTime').val() + '</div><div>Aggregation: ' +
$('#drpAggrInterval').find(":selected").text() +
'</div></span><span style="width:40px; display: inline-block; vertical-align: top;"><input id="btn' + ccount +
'" class="btnDelete" type="button" onclick="deleteTask(' + ccount + ')"/></span></div> ';
$('#divTravelTime').append(out);
var obj = { TaskId: ccount, TrajectoryName: $('#drpTrajectory').find(":selected").text(), StartTime: $('#txtStartTime').val(), EndTime: $('#txtEndTime').val(), Aggregation: $('#drpAggrInterval').find(":selected").text() }
tasks.push(obj)
ccount++;
$('#divTravelTimeHeader').show();
$('#divTravelTime').show();
$('#divJobSubmit').show();
if (tasks.length > 0) {
$('#divTaskView').show();
}
else {
$('#divTaskView').hide();
}
Code to delete an item from the JSON array :
function deleteTask(tt) {
var remID = "#" + tt;
$(remID).remove();
delete tasks[tt - 1];
if (tasks.length > 0) {
$('#divTaskView').show();
}
else {
$('#divTaskView').hide();
}
}