-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
67 lines (59 loc) · 1.99 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
60
61
62
63
64
65
66
67
//input elements
let inputName = document.getElementById("inputName");
let inputLastName = document.getElementById("inputLastName");
let inputDni = document.getElementById("inputDni");
let inputPhone = document.getElementById("inputPhone");
let inputMail = document.getElementById("inputMail");
let btnSubmit = document.getElementById("btnSubmit");
let form1 = document.getElementById("form1");
let textarea1 = document.getElementById("textarea1");
let unorderedList = document.getElementById("ulDatos");
let mostrarLista = document.getElementById("mostrarLista");
let limpiarLista = document.getElementById("limpiarLista");
//Objeto datos de usuario
let datosUsuario = [];
btnSubmit.onclick = (event) => {
event.preventDefault();
guardarDatos();
//form1.reset();
};
//Guardar datos dentro de local storage
const guardarDatos = () => {
datosUsuario = JSON.parse(localStorage.getItem("datosUsuario"));
datosUsuario.push({
nombre: inputName.value,
apellido: inputLastName.value,
dni: inputDni.value,
phone: inputPhone.value,
mail: inputMail.value,
});
localStorage.setItem("datosUsuario", JSON.stringify(datosUsuario));
};
mostrarLista.onclick = (evento) => {
crearLi();
};
const crearLi = () => {
let datosDeUsuarios;
datosDeUsuarios = JSON.parse(localStorage.getItem("datosUsuario"));
for (let loop = 0; loop < datosDeUsuarios.length; loop++) {
let newLi = document.createElement("li");
newLi.textContent =
datosDeUsuarios[loop].nombre +
" " +
datosDeUsuarios[loop].apellido +
" : " +
datosDeUsuarios[loop].phone +
" / " +
datosDeUsuarios[loop].mail;
unorderedList.appendChild(newLi);
}
};
limpiarLista.onclick = (evento) => {
console.log(unorderedList.childElementCount);
if (unorderedList.childElementCount !== 0) {
for (let loop = 0; loop <= unorderedList.childElementCount + 1; loop++) {
unorderedList.removeChild(unorderedList.lastChild);
}
unorderedList.removeChild(unorderedList.lastChild);
}
};