-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
67 lines (54 loc) · 1.53 KB
/
app.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const contactsContainer = document.querySelector(".contacts-lists");
const addNewContactBtn = document.querySelector(".add-contact");
let contactList = [
{
name: "Andrzej Duda",
phoneNumber: "500 100 235",
},
{
name: "Emanuel Macron",
phoneNumber: "134 456 551",
},
];
const renderContact = () => {
contactsContainer.innerHTML = "";
contactList.forEach((contact) => {
let newContact = `
<div class="contact">
<div class="contact-info">
<p>${contact.name}</p>
<span>${contact.phoneNumber}</span>
</div>
<div class="remove-img-wrapper">
<img alt="trash" src="/img/trash-bin.png"/>
</div>
</div>
`;
contactsContainer.innerHTML += newContact;
});
};
renderContact();
addNewContactBtn.addEventListener("click", (e) => {
e.preventDefault();
let nameInput = document.querySelector("#name");
let phoneInput = document.querySelector("#phone-number");
let newContact = {
name: nameInput.value,
phoneNumber: phoneInput.value,
};
contactList.push(newContact);
nameInput.value = "";
phoneInput.value = "";
renderContact();
});
contactsContainer.addEventListener("click", (e) => {
e.preventDefault();
if (e.target.tagName == "IMG") {
let trashes = [...document.querySelectorAll(".remove-img-wrapper img")];
let removeElement = trashes.indexOf(e.target);
contactList = contactList.filter((e, idx) => {
return idx != removeElement;
});
renderContact();
}
});