-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshopping-list.html
68 lines (57 loc) · 1.75 KB
/
shopping-list.html
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Shopping list example</title>
<style>
li {
margin-bottom: 10px;
}
li button {
font-size: 8px;
margin-left: 20px;
color: #666;
}
</style>
</head>
<body>
<h1>My shopping list</h1>
<div>
<label for="item">Enter a new item:</label>
<input type="text" name="item" id="item">
<button>Add item</button>
</div>
<ul>
</ul>
<script>
// handles for elements
const list = document.querySelector('ul');
const input = document.querySelector('input');
const button = document.querySelector('button');
button.onclick = function() {
let myItem = input.value;
console.log(myItem);
// var itemStore;
input.value = '';
// create elements and append to the parent
const listItem = document.createElement('li');
const listText = document.createElement('span');
const listButton = document.createElement('button');
listItem.appendChild(listText);
listText.textContent = myItem;
listItem.appendChild(listButton);
listButton.textContent = 'Delete';
list.appendChild(listItem);
listButton.onclick = function(e) {
list.removeChild(listItem);
}
input.focus();
};
// function to keep track of keydown events and display on console
input.addEventListener('keydown', (event) => {
console.log(`You pressed "${event.key}".`);
});
</script>
</body>
</html>