I’m working on an expense tracker. I just want to write it in a way where I can add three different input values: name (string), date (number) and amount (number) to a new row once I click the add expense button.
This is as far as I can get with JS because I’m not sure how to add tr to a table with 3 different input values.
Here’s my HTML:
<body>
<div class="container">
<h2>Expense Tracker</h2>
<h3>Add New Item</h3>
<div class="input-item">
<label >Name</label>
<input type="text" id="myName">
</div>
<div class="input-item">
<label>Date</label>
<input type="number" id="myDate">
</div>
<div class="input-item">
<label>Amount</label>
<input type="number" id="myAmount">
</div>
<div class="button">
<button type="button" id="myButton">Add Expense</button>
</div>
<table class="table" id="myTable">
<tr>
<th>Name</th>
<th>Date</th>
<th>Amount</th>
<tr/>
<tr>
</tr>
</table>
</div>
</body>
Here’s my JS
const nameInput = document.getElementById("myName");
const dateInput = document.getElementById("myDate");
const amountInput = document.getElementById("myAmount");
const addButton = document.getElementById("myButton");
const expenseTable = document.getElementById("myTable");
addButton.addEventListener('click', function(event){
const name = nameInput.value;
const date = dateInput.value;
const amount = amountInput.value;
});