<html>
<head>
<title>Expense Tracker</title>
<style type="text/css">
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button
{
-webkit-appearance: none;
margin: 0;
}
table
{
display: none;
}
td,th
{
text-align: center;
}
th,td
{
border: 1px solid black;
}
</style>
</head>
<body>
<h1>Expense Tracker</h1>
<form action="">
<label for="date">Date: </label>
<input type="date" id="date">
<br>
<label for="desc">Description: </label>
<input type="text" id="desc">
<br>
<label for="amount">Amount: </label>
<input type="number" id="amount">
<br>
<input type="button" id="submit" value="Submit">
<br>
</form>
<table id="table">
<tr>
<th>Date</th>
<th>Description</th>
<th>Amount</th>
</tr>
</table>
<div id="total"></div>
<script type="text/javascript">
var total = 0;
document.getElementById("submit").onclick=function()
{
document.getElementById("table").style.display="block";
var table = document.getElementById("table");
var row = table.insertRow(-1);
var date = row.insertCell(0);
var desc = row.insertCell(1);
var amt = row.insertCell(2);
date.innerHTML = document.getElementById("date").value;
desc.innerHTML = document.getElementById("desc").value;
amt.innerHTML = document.getElementById("amount").value;
total += document.getElementById("amount").value;
document.getElementById("total").innerHTML = 'Total: ' + total;
return false;
}
</script>
</body>
</html>
I am trying to insert the input given in the form into the table after the submit button is clicked and print the total amount after that particular entry. But here the amount is just appended to the total as in a string. Eg- The initial value of total is 0 and when amount is 2 the total should become '2' but instead, it becomes '02'. How do I fix this?
