-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
45 lines (34 loc) · 1.32 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
document.getElementById('addItemButton').addEventListener('click', addItem);
function addItem() {
const itemName = document.getElementById('itemName').value;
const itemQuantity = document.getElementById('itemQuantity').value;
if (itemName === '' || itemQuantity === '') {
alert('Please enter both item name and quantity.');
return;
}
const pantryList = document.getElementById('pantryList');
const row = document.createElement('tr');
row.innerHTML = `
<td>${itemName}</td>
<td>${itemQuantity}</td>
<td class="actions">
<button onclick="deleteItem(this)">Delete</button>
<button onclick="editItem(this)">Edit</button>
</td>
`;
pantryList.appendChild(row);
document.getElementById('itemName').value = '';
document.getElementById('itemQuantity').value = '';
}
function deleteItem(button) {
const row = button.parentElement.parentElement;
row.remove();
}
function editItem(button) {
const row = button.parentElement.parentElement;
const itemName = row.cells[0].innerText;
const itemQuantity = row.cells[1].innerText;
document.getElementById('itemName').value = itemName;
document.getElementById('itemQuantity').value = itemQuantity;
deleteItem(button);
}