Event Bubbling and Capturing in JavaScript
Вставка
- Опубліковано 29 вер 2024
- Understanding Event Bubbling and Event Capturing phase in DOM and how to use it.
Event Bubbling
------------------------
Event bubbling directs an event to its intended target, it works like this: A button is clicked and the event is directed to the button. If an event handler is set for that object, the event is triggered. If no event handler is set for that object, the event bubbles up (like a bubble in water) to the objects parent.
*My Udemy Courses
www.udemy.com/...
www.udemy.com/...
Follow me for technology updates
* / techsith
* / techsith
* / techsith1
* / 13677140
* / patelhemil
Help me translate this video.
* www.youtube.co...
Note: use translate.goog... to translate this video to your language. Let me know once you do that so i can give you credit. Thank you in advance.
stopPropagation() method is used to stop event bubbling and event capturing..
yes that is correct. however you have to use it in one of the events.
You are correct, however, the stopPropagation() method is not recommended. It may screw with UX people and has may induce collateral damage.
Thank you for this tutorial on Bubbling and Capturing. Very helpful!!
really good explanation thank you i was so confused about event phases but now it's clear to me
Cool, clear explanation :)
Glad it was helpful!
Very informative, clearly explained
really helpful thanks..
Glad to hear that! Thanks for watching!
awesome now I understood capturing and bubbling both thanks a alot :)
:) thanks for watching!
thank you for sharing, i didnt know event capturing parameter existed
Somehow jquery didnt have that option, and since most of us dont use vanilla javaScript we dont know certain options available. but i think its a useful trick to know. Thanks for watching! :)
Awesome explanation! thanks
I like your videos! They are very helpful!
Thanks for watching :)
You should first "show" bubbling and capturing (maybe with elements that are highlighted instead of or in addition to console logging.). You should also give real world examples like showing how it works with a nav bar and some links. You should also show how it works with stoppropagation.
Great explanation, thank you.
thanks it helped me
It's Just amazing.
Thanks for watching Pradeep!
Cool explaination, thanks
Thanks for the video! How does this combine with stopPropagation() and preventDefault()? Can I stop bubbling and event capturing with both? Or just with stop propagation?
What programs do you use to make these videos? (I mean that you can add jquerry by only one click, and you have such an interesting list of modules.)
Really interested.
Do you use jsfiddle?
yes, I use JsFiddle you can add most of the library and even babel with a click.
Explained very well! Thank you
Thanks for watching! :)
nice, thanks.
Thanks for watching ! :)
Which Code Editor is being used in the video?
Can you explain it in real world project example because I am pretty new student ,so these concepts I completely got it but can’t relate in real world project
Azam, any UI intensive app, you will be using it a lot. for an example, lets say you want to show a menu on right click in your app. except one place inside a text box where right click would allow you to format the text. so the first right click is at body level and the last right click is at the lowest level in that text box. But when you right click on the text box you want that to execute first and you want to ignore the body right click. This is a classic example of how event bubbling works.
What if I define both Capture and Bubble event
Happy Makar Sankaranti
boss can you give github link inside your description box, that could be nice
Great explaination, better if you would have covered stop propogation
Thanks for watching Sumer!
Just add stopPropagation() on below code and pass an event e on the function.
c.addEventListener(
'click',
function(e) {
// e instance of an event which is the current event
e.stopPropagation();
console.log('clild clicked');
},
fromCapture
);
Happy Makar Sankaranti
Happy Makar Sankranti!
var length = 10;
function fn() {
console.log(this.length);
}
var obj = {
length: 5,
method: function(fn) {
fn();
arguments[0]();
}
};
obj.method(fn, 1);
Output will be => 10,2
()=> i want to ask why i am getting 2 for this?
Really helpful! Thanks!
Nice, but needs a example too me thinks.
Best teacher on UA-cam hands down
May The Fourth be with you. Thanks for the video!
thank you very much!
There are too many videos repeating the same thing about bubbling and capturing, they are almost the same videos. I'd suggest you make a video to highlight the impact of 'bubbling' and 'capturing' in a real-world practical scenario with 2 examples to highlight the contrast, the problem that you are facing, and how to resolve it with either 'capturing' or 'bubbling'.
So now I am imagining, what could be use case when we actually want to use the event bubbling? To my simple novice js dev mind, event bubbling sounds like a side effect and should always be prevented! ?? Any more insights ?
I want write calculators.html using JavaScript. How to identifying clicked elements and set text value.
Nice.
ah shit here we go again
I like the smiley faces! Also, very solid explanation of bubbling and capturing.
why would use jquery for this tutorial? jquery is a library its not even programming...
Can I get example of bubbling &capturing without using jQuery??
Fantastic video😍👍
World's best teacher :)
Awesome dude. Thanks for the explanation.
thanks for the video. really cool. i understand the semantics, but what is the rationale for event bubbling in JS... i mean why do we have it
lets say you have a webpage where if you right click you want to show some menu. except part of the small area where you want to have a different menu or no menu. Basically your body element has a right click event that would execute when you click anywere on the body. and the small area , which is an element under the body , also has a right click event . so when you click on that small area it would check if that has right click event before going to higher level of elements so you can prioritize which one will execute first.
thanks for explaining on the use of event bubbling and capturing. please mention the usage of it in the videos that you record. it will help us better understand the concepts
very well explained Event Bubbling and Event Capturing
Explaination is very well! Thank you
Thanks for the video, nicely explained.
props to that explanation clearer than a transparent bravo
Nice video i learn 2 new things in JavaScript it's really helpful
I am glad you learnt . Keep it up Rahul
Which work environment are you using?
Thank you so much! This video finally helped me understand event bubbling, especially on your example at 4:34.
Glad it helped!
I'm 70 years old and retired. So I figured that I send emails to friends ,why not learn something about how the computer works. A young man said lean about Html ,CSS and Javascript. WOW!! somebody needs to smack me.
I figured it was not easy ,but never expected it to be rocket science. hahahaha this video did explain an interesting point. Hope to some day understand most of it.
Namron, I admire your desire to learn something new at 70. HTML , CSS and JavaScript can be confusing . Feel free to ask questions if you have any.
Very helpful and clear. Thanks a lot.
I'm confused. If capturing is the default behaviour in modern browsers then how come, when you wrote the JS code, bubble is being performed?
Bubble is the default behavior of the modern browser. Sorry if I said capture, I might have made a mistake. thanks for reminding.
really awesome tutorial ever.. I watched....
Thanks for watching ! :)
youuu... my friend!!! are way too underrated!
where is spec for all this hidden things?
Thank you for the explanation.
lots of children and parents executed
Thank you sooooooooo muchhhh.....u hv jst cleared the concept that was I'm looking for so many days 👍
Most welcome 😊
Thnx alot it was really helpful
u cud have added stop propogation also
thank you ... very helpful ;
I guess, for me, the obvious question is, why should click events be executed on buttons we never clicked on?
Actually you did click on . for example if you are inside the bathroom you are also inside the house. so you clicked on the button which means you also clicked on the page.
Greate Tutorial, Thanks
HI Sir .. You are really helping a lot in understanding the depth of the concepts of JavaScript...
Coool. This video made everything clear.Thanks ..
Thank you for watching!
is capturing similar to delegation ? and by default Bubbling happens or delegation?
Oldest javascript teacher
Don't think I've seen a video on event bubbling until now. Every time it's explained to me I forget the explanation, now I just press 'repeat'!
Actually I referring to the call stack, (similar concept?).
call stack is diffrent. its how you execute each statement in javascript last statement gets executed first . However in the event bubbling there events gets executed from lower level elements to top level elements inside the dom.
Good clear explanation!
I love your channel. Quick question a bit off topic. Which video editor do you use to capture the video of you in the bottom corner, and to record your text editor?
A Constructive Comment
Perfectly described
I want to see the real project example of Event Bubbling and Event Capturing, to understand how it works in real scenario
Man this intro pic 😂
Thank you very much, I feel like I am walking away with a much better understanding of event flows.
Thanks a lot....❤
Amazing videos very useful and knowledge gaining..
Thanks for watching!
Found out this channel lately. All explanations are clear, precise and easy to understand
Should we do something to stop event bubbling? What could be the consequences if we don't stop it? If we have to stop it then how?
you can use event.stopPropagation();
what about window.event.cancelBubble is that the same thing?
Its a same thing, cancelBubble is an old method used only by older version of IE, I believe earlier than IE9 .
techsith ok cool as i suspected because its in the microsoft exam awesome channel
ok. avoid using it. :)
Great Explanation in simple words awesome :)
THanks for watching!
Thanks a ton!
Very well explained, thank you :)
It is incomplete you didnt say how to stop thebpropogation..
Sir please explain prevent default and stop propogation with example
Thank you
Nice...sir
Clear and clean explanation with no unnecessary stuff
Thanks
Hi, I’m new to js and thanks for explaining it very well. Can you also tell me which text editor you are using?
Hey, how's it going? I have a problem here. "Event Bubbling.js:25 Uncaught TypeError: Cannot read property 'addEventListener' of null" why doesn't it recognize this? can you help me please?
may be the element doesn't exist . Can you send me the code.?
@@Techsithtube Yes, thank you. That's for the HTML:
child
other
That's for Javascript:
$('#parent').click(function(){
console.log('parent clicked');
});
$('#child').click(function(){
console.log('child clicked');
});
var p = document.querySelector('#parent');
p.addEventListener('click', function(){
console.log('parent clicked');
});
var c = document.querySelector('#child');
c.addEventListener('click', function(){
console.log('child clicked');
});
for the javaScript part wrap the whole content in an iife like this. If you dont know what iife is i have a tutorial on it.
$(function() {
$('#parent').click(function() {
console.log('parent clicked');
});
$('#child').click(function() {
console.log('child clicked');
});
var p = document.querySelector('#parent');
p.addEventListener('click', function() {
console.log('parent clicked');
});
var c = document.querySelector('#child');
c.addEventListener('click', function() {
console.log('child clicked');
});
})
@@Techsithtube Great Thank you !
I Liked this n posted it on Facebook! U have an awesome channel!
awesome tutorial ! great big thanks
Thanks for watching!
Very helpful. Thanks for your time.
That was good explanation.
Could you please answer below questions?
How to stop bubbling?
What is difference between stopPeopagation and preventDefault?
What is use of cancelBubble?
Also we are waiting for your new videos on interview questions to experienced developer.
Thank you
you can stop bubbling using stopPropagation and cancelBubble. they are almost the same. preventDefault stop . PreventDefault stops the browsers default action. I will create a interview Questions for Experienced Developer soon.
Could you please tell me how you do that? by writing "div#main" it will automatically print please tell me. By the way i like all your expalnation....Its really too good. Thank you for providing such a great explanation.
you type div#main and hit tab. most of the editors support that.
Thanks for the video. What would be great is if you created a series of videos that incorporated all of what your advanced js tutorials covered but targeted to a specific outcome/product. For example, when creating a single page app (not using any SPA frameworks) that is targeted for mobile, how one structures the HTML, writes the JavaScript, etc matters a great deal to ensure the app performs as well as a native app and does not crash. Memory management is a very important consideration and it would be very helpful if you were to show the impacts of code on memory allocation/use, what the garbage collector is supposed to do and whether or not it can do its job based on how the code is written, and how to use the Chrome Dev Tools to identify memory use, memory leaks and how to troubleshoot accordingly.
That is a great idea . I will defiantly spend sometime creating a practical project series that will give overall picture. Perhaps a small library. I will try to think of something but if you have any project ideas, let me know. Thanks
Good Tutorial Keep up
Which IDE are you using?
JS fiddle