-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathchetan_event_deligation.js
More file actions
106 lines (81 loc) · 2.36 KB
/
chetan_event_deligation.js
File metadata and controls
106 lines (81 loc) · 2.36 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
<div class="parent">
<div class="child child1"></div>
</div>
*/
const parent = document.querySelector('.parent');
const child = document.querySelector('.child1');
// Event Propagation
// with capturing phase
child.addEventListener('click', (e) => {
console.log('child');
}, true);
parent.addEventListener('click', (e) => {
e.stopPropagation();
console.log('parent');
console.log(1 + 3);
}, true);
// with bubbling phase
// child.addEventListener('click', (e) => {
// e.stopPropagation();
// console.log('child');
// }, false);
// parent.addEventListener('click', (e) => {
// console.log('parent');
// console.log(1 + 3); // heavy computation which we don't want to called again :D
// }, false);
// Event Deligation
parent.addEventListener('click', (e) => {
e.stopPropagation();
console.log(e.target);
const elem = e.target;
if (elem.classList.contains('card-button')) {
console.log(elem.dataset.idProductData);
}
}, true);
const createCard = ({ id, title, thumbnail }) => {
return `<div id=${id} data-id-product-data=${id} class="product">
<h1>${title}</h1>
<img src=${thumbnail} />
<button class="card-button" data-id-product-data=${id}>Show More</button>
</div>`;
}
const renderData = (products) => {
console.log(products);
const html = products.map((product, index, array) => {
return createCard(product);
}).join('');
parent.innerHTML = html;
}
console.log(this);
const fetchData = async () => {
try {
const res = await fetch(`https://dummyjson.com/products/`);
const data = await res.json();
console.log(data);
const products = data.products;
renderData(products);
} catch (error) {
console.log(error);
}
}
fetchData();
// const obj = {
// brand: "Apple",
// category: "smartphones",
// description: "An apple mobile which is nothing like apple",
// discountPercentage: 12.96,
// id: 1,
// array: [1, 2, 3]
// }
// const arr = [1, 2, 3];
// const [first, , third] = arr;
// console.log('arrr ', first, third);
// const { discountPercentage: discount, brand, category, chetan } = obj;
// console.log();
// // const discount = obj.discountPercentage;
// const fun = () => {
// const fun1 = () => { return 2 };
// return [fun1];
// }
// const [func] = fun();