-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
82 lines (61 loc) · 2.1 KB
/
main.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
var form = document.getElementById("my-form");
// form submit event
form.addEventListener("submit", addItem);
function addItem(e) {
e.preventDefault();
//get input value
var newName = document.getElementById("name").value;
var newEmail = document.getElementById("email").value;
//put in local storage
// localStorage.setItem("name", newName);
// localStorage.setItem("email", newEmail);
let infoObj = {
name: newName,
email: newEmail,
};
let infoObj_serialized = JSON.stringify(infoObj);
localStorage.setItem(infoObj.email, infoObj_serialized);
let infoObj_deserialized = JSON.parse(localStorage.getItem("infoObj"));
// Add event for delete
//form.addEventListener('click', removeItem);
//create new li element
var data = document.createElement("li");
// Add class
document.getElementById("name").value = '';
document.getElementById("email").value = '';
data.className = "list-group-item";
//Create a delete button
var deleteBtn = document.createElement("input");
deleteBtn.type = "button";
deleteBtn.value = "delete";
deleteBtn.onclick = () => {
localStorage.removeItem(infoObj.email);
form.removeChild(data);
};
//add classes to delete button
deleteBtn.className = "btn-delete";
//Append Text node to delete
deleteBtn.appendChild(document.createTextNode("X"));
//Create a edit button
var editBtn = document.createElement("input");
editBtn.type = "button";
editBtn.value = "edit";
editBtn.onclick = () => {
document.querySelector("#name").value = infoObj.name;
document.querySelector("#email").value = infoObj.email;
localStorage.removeItem(infoObj.email);
form.removeChild(data);
};
//add classes to edit button
editBtn.className = "btn-edit";
//Append Text node to delete
deleteBtn.appendChild(document.createTextNode("X"));
//add text node with input value
//add text node with input value
data.appendChild(document.createTextNode(newName));
data.appendChild(document.createTextNode(": " + newEmail));
//append child
data.appendChild(editBtn);
data.appendChild(deleteBtn);
form.appendChild(data);
}