-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBubbling&Capturing.js
More file actions
38 lines (29 loc) · 1.31 KB
/
Bubbling&Capturing.js
File metadata and controls
38 lines (29 loc) · 1.31 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
//keypress event
const Body=document.body;
//pressed key info will be return
Body.addEventListener('keypress',(e)=>{
console.log(e);
console.log(e.key);
});
const child=document.querySelector('.child');
child.addEventListener('mouseover',()=>console.log('mouse over event occured'));
child.addEventListener('mouseleave',(e)=>console.log(e,'mouse leave event occured'));
//event bubbling/propogation
//event capturing
//event deligation
const grandparent=document.querySelector('.grandparent');
const parent=document.querySelector('.parent');
grandparent.addEventListener('click',()=>console.log('you clicked on grandparent'));
//true will capture event first
parent.addEventListener('click',()=>console.log('clicked on parent'),true)
child.addEventListener('click',()=>console.log('clicked on child element'))
//when the 3rd argument is true the even will start capturing from parent -> child
//when the 3rd argument is false/default the even will start capturing from child -> parent
// EVENT DELIGATION
//when the event can be calle by single element to for all of its child element
//using e.target to find the event location from where it is getting fired
grandparent.addEventListener('click',(e)=>{
console.log(e)
console.log(e.target);
console.log(e.target.textContent);
});