Hello everyone..
I'm developing a calorie counter with a <select> tag as my list of foods. It currently calculates the number of servings, calories per serving as well as the daily caloric intake.
My problem is I implemented a "delete row" function to delete a food in case a user has inputted it by mistake, the problem is it does not deduct correctly from the total..
I think the problem is in the loop. anyway, here is the code:
function AddToList()
{
var passingvalue = document.serving.foods.value
DCI = document.serving.DCI.value
var splitArr = passingvalue.split("|");
var servingSize = splitArr[0];
var Calories = splitArr[1];
var foodChoice = document.serving.foods.options[document.serving.foods.selectedIndex].text;
var newRow = document.getElementById("InTake").insertRow();
var oCell = newRow.insertCell();
var element1 = document.createElement("input");
element1.type = "checkbox";
oCell.appendChild(element1);
oCell = newRow.insertCell();
oCell.innerHTML = foodChoice;
oCell = newRow.insertCell();
oCell.innerHTML = servingSize
oCell = newRow.insertCell();
oCell.innerHTML = document.getElementById('noserving').value;
oCell = newRow.insertCell();
oCell.innerHTML = Calories
oCell = newRow.insertCell();
var servingNo = document.getElementById("noserving").value
var total = Calories * servingNo
oCell.innerHTML = total
UsedCalories = UsedCalories + total
remainingCalories = DCI - UsedCalories
document.getElementById("CalTotal").innerHTML = UsedCalories
document.getElementById("CalRem").innerHTML = remainingCalories
}
function SaveData()
{
createRequest();
var url = "SaveData.asp?Rem=" + escape(remainingCalories) + "&DCI=" + escape(DCI)+"&RandomKey=" + Math.random() * Date.parse(new Date());
request.open("GET", url, true);
request.onreadystatechange = ShowRemaining;
request.send(null);
alert("data sent");
}
function ShowRemaining()
{
var fullString;
if (request.readyState == 4)
{
if (request.status == 200)
{
alert("working");
fullString=request.responseText;
alert(fullString);
}
}
}
the delete row function
function deleteRow(InTake) {
try {
var table = document.getElementById(InTake);
var currentTotal = document.getElementById('UsedCalories');
var rowCount = table.rows.length;
var dt = 0;
for(var i=0; i<rowCount; i++){
var row = table.rows[i];
var chkbox = row.cells[0].childNodes[0];
var ns = row.cells[3].childNodes[0].nodeValue;
var cps = row.cells[4].childNodes[0].nodeValue;
dt = ns * cps;
if(chkbox != null && chkbox.checked == true){
table.deleteRow(i);
rowCount--;
i--;
}
}
alert(dt);
}
catch(e)
{
alert(e);
}
alert('yay');
var updated = UsedCalories - dt;
alert(updated);
document.getElementById("CalTotal").innerHTML = updated;
var updatedRem = remainingCalories + dt;
document.getElementById("CalRem").innerHTML = updatedRem;
}