-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
70 lines (58 loc) · 1.88 KB
/
app.js
File metadata and controls
70 lines (58 loc) · 1.88 KB
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
const cartBtn = document.querySelectorAll('.agregarBtn');
const vaciarCart = document.getElementById('vaciarCart');
vaciarCart.addEventListener('click', vaciarCarrito, true);
cartBtn.forEach((element) => {
element.addEventListener('click', (e) => {
let img = element.parentElement.parentElement.parentElement.previousElementSibling.firstElementChild.firstElementChild.src;
let contenido = element.previousElementSibling.textContent;
let precioPos = contenido.indexOf('$');
let nombre = contenido.slice(0, precioPos);
let precio = contenido.slice(precioPos);
let pos = img.indexOf('img') + 3;
let partPath = img.slice(pos);
const obj = {};
obj.img = `img-cart${partPath}`;
obj.nombre = nombre;
obj.precio = precio;
imprimirObj(obj);
})
});
function imprimirObj(obj) {
let tabla = document.querySelector('table tbody');
let fila = document.createElement('tr');
fila.innerHTML = `
<td>
<figure class="image is-48x48">
<img src="${obj.img}" alt="${obj.nombre}">
</figure>
</td>
<td>
${obj.nombre}
</td>
<td class="espaciadoPrecios">
${obj.precio}
</td>
<td>
<span class="icon is-medium">
<a class="trash"><i class="far fa-trash-alt"></i></a>
</span>
</td>`;
tabla.appendChild(fila);
registrarProducto();
}
function registrarProducto() {
let trash = document.querySelectorAll('.trash');
console.log(trash)
trash.forEach(element => {
element.addEventListener('click', (e) => {
let fila = e.currentTarget.parentElement.parentElement.parentElement;
fila.remove();
})
})
}
function vaciarCarrito() {
let tbody = document.getElementById('tablebody');
while(tbody.hasChildNodes()){
tbody.removeChild(tbody.firstChild);
}
}