-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
59 lines (47 loc) · 1.76 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
50
51
52
53
54
55
56
57
58
59
var listElement = document.querySelector('#app ul');
var linkInputElement = document.getElementById('link');
var nomeInputElement = document.getElementById('nome');
var buttonAddElement = document.querySelector('#app button');
var lista = JSON.parse(localStorage.getItem("savedList")) || []
function renderLista() {
listElement.innerHTML = ''
for (item of lista) {
var itemElement = document.createElement('li');
var linkElement = document.createElement('a');
var buttonElement = document.createElement('button');
var buttonText = document.createTextNode('Excluir');
var nomeText = document.createTextNode(item.nome);
buttonElement.appendChild(buttonText);
linkElement .appendChild(nomeText);
linkElement .setAttribute("href", item.link);
buttonElement.setAttribute("onclick", `deleteLink(${lista.indexOf(item)})`);
itemElement.appendChild(linkElement);
itemElement.appendChild(buttonElement);
listElement.appendChild(itemElement);
}
}
renderLista();
function addLink() {
if (linkInputElement.value && nomeInputElement.value){
var linkText = linkInputElement.value;
var nomeText = nomeInputElement.value;
lista.push({link: linkText, nome: nomeText});
var inputList = document.querySelectorAll('input');
for (input of inputList){
input.value = '';
}
renderLista();
saveStorage();
} else {
alert('Preencha os campos');
}
}
buttonAddElement.onclick = addLink;
function deleteLink(posicao){
lista.splice(posicao, 1);
renderLista();
saveStorage();
}
function saveStorage(){
localStorage.setItem("savedList",JSON.stringify(lista));
}