-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
49 lines (46 loc) · 1.66 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
46
47
48
49
// accessing elements
const inputBox = document.querySelector('input')
const btn = document.querySelector('button')
const itemList = document.getElementById('listItems')
const addItemBtn = document.getElementById("addItemBtn")
// const itemListArea = document.getElementById("listItems")
const newItem = document.getElementById('newItem')
// add event to each button element button will only work if there is value
btn.addEventListener("click", () => {
if (inputBox.value !== "") {
const listItem = document.createElement('li');
listItem.classList.add('eachItem')
listItem.id = "newItem"
const inputText = inputBox.value;
const listArea = document.createElement('div')
listArea.id = "strike"
listArea.id = "eachItem"
listArea.id = "listItem"
listArea.textContent = inputText
const deleteBtn = document.createElement('button')
deleteBtn.textContent = "Delete";
deleteBtn.classList.add("deleteBtns")
listItem.appendChild(listArea)
listItem.appendChild(deleteBtn)
itemList.appendChild(listItem)
inputBox.value = ""
} else {
alert("You did not enter an item. Please try again.")
}
})
// enables user to hit "enter" instead of having to click; set to only work when input has value
inputBox.addEventListener("keypress", (event) => {
if (event.key === "Enter" && inputBox.value) {
event.preventDefault()
document.getElementById("addItemBtn").click();
}
})
// add event to each item in list
// prompts user to confirm deletion of item
itemList.addEventListener("click", (event) => {
if (event.target.nodeName === 'BUTTON') {
if (confirm("Are you sure you want to delete this item?")) {
itemList.removeChild(event.target.parentNode)
}
}
})