-
Notifications
You must be signed in to change notification settings - Fork 0
/
leads.js
59 lines (44 loc) · 1.44 KB
/
leads.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
const input = document.querySelector("input#input-el");
const saveButton = document.querySelector("button#save-btn");
const linkBody = document.querySelector("ul#link-body");
const deleteButton = document.querySelector("button#delete-btn");
const setMultipleAttrs = ( el, obj ) => {
for ( attr in obj ) {
el.setAttribute(attr, obj[attr]);
}
}
const storeLeads = ( ul ) => {
let leads = [];
for ( el of ul.children ){
leads.push(el.textContent);
}
localStorage.setItem("leads", JSON.stringify(leads))
console.log(leads);
}
const leadsFromStorage = JSON.parse(localStorage.getItem("leads"));
const render = ( store ) => {
let innerStoreHTML = "";
for (let i = 0; i < store.length; i++) {
innerStoreHTML += `
<li><a href=${store[i]} target=_blank >${store[i]}</a></li>
`
}
linkBody.innerHTML = innerStoreHTML;
}
if ( leadsFromStorage ) {
render( leadsFromStorage );
}
saveButton.addEventListener( "click", () => {
const link = document.createElement("a");
const li = document.createElement("li");
link.textContent = input.value;
input.value = "";
setMultipleAttrs(link, {"href": input.value, "target": "_blank"});
li.appendChild(link);
linkBody.appendChild(li);
storeLeads(linkBody);
});
deleteButton.addEventListener( "dblclick", () => {
localStorage.clear();
linkBody.innerHTML = "";
})