Event Delegation in Javascript | UI/Frontend Interview Question

Поделиться
HTML-код
  • Опубликовано: 1 окт 2024
  • - What is Event Delegation?
    - A quick demo and examples of this techniques
    - Benefits of Event Delegation?
    - Limitations of Event Delegation?
    Link to code of Example 1 - codepen.io/aks...
    Link to code of Example 2 - codepen.io/aks...
    If you get to learn something from this video then do give it a thumbs up and subscribe to my channel for more such videos.
    If you want me to cover any specific topic, then comment down below. I would be happy to help you.
    If you find my videos helpful,
    then please support this channel by buying a coffee,
    www.buymeacoff...
    Cheers,
    Akshay Saini
    akshaysaini.in
    Would love to Stay Connected with you ❤️
    LinkedIn - / akshaymarch7
    Instagram - / akshaymarch7
    Twitter - / akshaymarch7
    Facebook - / akshaymarch7
    #Javascript #JavascriptInterviewQuestions #AkshaySaini

Комментарии • 157

  • @Abhishek-dp5tc
    @Abhishek-dp5tc 2 года назад +87

    Benefits of event delegation:
    1) improves memory space
    2) mitigates risk of performance bottle neck
    3) Dom manipulation
    4) when elements get added dynamically, the process of adding events is slow
    Limitations:
    1) All the events are not bubbled up, some events like blur, focus are not bubbled up
    2) if e.stopPropogation is used in child, then events are not bubbled up

  • @cnsnmms3708
    @cnsnmms3708 3 года назад +58

    Eventually found someone who really knows something and shares it on youtube :D

  • @shadestorm3995
    @shadestorm3995 3 года назад +33

    I have been adding listeners on loop like an idiot when I could have just done this...
    This was enlightening to a great degree.

    • @mridulkumar4087
      @mridulkumar4087 3 года назад +3

      Your are even adding a loop but I have been adding a event listener each and every time for every element

    • @jamesonvincent3779
      @jamesonvincent3779 3 года назад

      i know im randomly asking but does someone know of a trick to log back into an instagram account?
      I stupidly forgot my password. I would appreciate any tricks you can give me!

  • @ShouvikMitraPlus
    @ShouvikMitraPlus 5 лет назад +35

    Nice one! also got to know how frameworks make use of custom attributes like data-dismiss="modal" in bootstrap. Thanks!

    • @akshaymarch7
      @akshaymarch7  5 лет назад +51

      You're absolutely right Souvik and the people who write frameworks are just like us, it's just that they understand these core concepts very well and how things work under the hood. Keep on learning these basic concepts and slowly everything will make sense and you will get a lot more wow moments. :)

    • @Abhishek-do8mp
      @Abhishek-do8mp 2 года назад +1

      @@akshaymarch7 ab to aajao sir

  • @BaishaliGhosh13
    @BaishaliGhosh13 4 года назад +13

    I have been terrified of frontend questions in fullstack positions but your videos have given me so much clarity and insight into the working of JS. Thanks a lot Akshay.

  • @user-uh3zr7mo4i
    @user-uh3zr7mo4i 5 лет назад +10

    Dude! I love you . These videos are really helpful for self taught devs.

  • @shishirsonekar5661
    @shishirsonekar5661 4 года назад +6

    Thanks, Akshay for this perfect explanation...
    Also,
    Being a full-stack developer and like me, a lot many FS devs keep ignoring CSS because it needs a proper understanding of not just the target element as well as the effects passed on from parents in the hierarchy. Overall, CSS being a secondary skill for this reason as well, it's not focused much in tech Interviews. But, I feel it is much-required skills to deliver a quality products. If you've cracked CSS properties' relations with its parents in the hierarchy, I'm sure huge junta including me would like to learn it from you!...

  • @saritajha1
    @saritajha1 3 года назад +4

    You are absolutely right !! it opened my eyes when I actually understood this concept :)
    No tutorial/ video ever explained delegation this way. Can't thank more Akshay.
    God bless you.

  • @postive_thoughts1307
    @postive_thoughts1307 Месяц назад

    Hi Akshay, why we are using if(e.target.tagName == "LI") that is not clear to me ? please Reply

  • @abhishekjain383
    @abhishekjain383 2 месяца назад

    Hi Akshay Bhaiya,
    I have one doubt about how we can say that we are using event bubbling here, not event capturing.
    P.S. I have already watched event bubbling and event capturing videos.

  • @sowmithreddy18
    @sowmithreddy18 4 года назад +15

    Never knew data attributes could be used this way. Amazing !

  • @manojkumarsaheng
    @manojkumarsaheng Год назад

    Thx more knowledge video but Request video Virtual DOM and Actual DOM

  • @hardikluhar4738
    @hardikluhar4738 3 года назад +3

    These videos are really helpful for self-taught devs.

  • @vandanachintala8326
    @vandanachintala8326 4 года назад +3

    Thanks Akshay for awesome explanation of each topic. I have gone through your event bubbling and capturing video before this one. I think your first example regarding categories is the best example of enabling event capturing. Since we are attaching event handlers to parent - instead of events bubbling from child to parent, we can enable useCapture property and can stop delegation to child elements for better performance using e.stopPropagation().

    • @kirankamath5891
      @kirankamath5891 3 года назад

      but how we will understand from where the event actually occurred if the target is the child element

  • @hundredeyes7759
    @hundredeyes7759 5 лет назад +3

    Finally I got it!!. is this the same way how react-router work? i mean like not exactly the same but core concepts are this only right?

    • @akshaymarch7
      @akshaymarch7  5 лет назад +1

      I don't know exactly about it. React router is all together a giant concept, won't be this simple. But yeah this concept is often used for setting behaviour patters in the code :)

  • @priyaranjan1733
    @priyaranjan1733 5 лет назад +1

    In CSS, we use as event delegation mean, we assign our base tag's href for all tags in that page. This is just analogy for those who know but not Event Delegation. 😕

  • @sahilsatishkumar
    @sahilsatishkumar 4 года назад +1

    So I am not the only person who is reminded of the time by them machines

  • @ryansantiago3684
    @ryansantiago3684 Год назад +1

    No joke, you are the best person when it comes to explaining. I really recommend you Akshay! Keep it up!

  • @jayanthbabusomineni557
    @jayanthbabusomineni557 5 лет назад +1

    Please make video on closures in JavaScript

  • @tusharkrishna8149
    @tusharkrishna8149 4 года назад +1

    Your explanation is too good, can you make a video on java script timers please

  • @PeaceUrSoul
    @PeaceUrSoul 4 года назад +1

    Hi Akshay, thanks for bringing such insights logics. Keep post such videos..

  • @leolowe22
    @leolowe22 4 года назад +2

    where does the bubbling stop up in the hierarchy?

  • @onlynaveen
    @onlynaveen 4 года назад +2

    Hi sir, after 1 year learning you lesson me a practical and real things, Thanks for advise and teach.

  • @kiranmahajan778
    @kiranmahajan778 6 месяцев назад

    In event delegation we would have to write a lot of if and else condition to handle different click events

  • @navdeepsr93
    @navdeepsr93 4 года назад +1

    Thanks Akshay , you explained very nicely . :)

  • @shoaib_akhtar_1729
    @shoaib_akhtar_1729 3 месяца назад

    Hats off, awesome teaching skills.

  • @SAIRAM-ct5cy
    @SAIRAM-ct5cy 3 года назад +1

    Good job Akshay..its amazing how you present the concepts precisely.Keep up the good work!!

  • @prabu2778
    @prabu2778 4 года назад +1

    You are great !!!!!!?
    Helping so many poor people

  • @samdatta9163
    @samdatta9163 4 года назад +1

    Good Quality and informative video.I kept all the concepts in my mind for interview.Thanks so much.

  • @utpalclicks
    @utpalclicks 4 года назад +10

    And for the last example, we can merge it with "denounce" so that the event only triggers once the user stop typing... 😀
    You explained this very well... Thanks!!

    • @aadispare3673
      @aadispare3673 Год назад +1

      Nope. Debounce on input field validation is a very bad idea.
      You need to notify the user as soon as he types something against the rules, instead of letting him know after he's finished typing a long sequence of mistakes. That is bad user experience.

  • @VinayVerma22
    @VinayVerma22 5 лет назад +1

    Very nice and precise tutorial with great examples. Requesting you to make a video on "Event Loop". :)

  • @PeaceUrSoul
    @PeaceUrSoul 4 года назад +4

    At 8:50 , 21 hours of what??

    • @akshaymarch7
      @akshaymarch7  4 года назад +2

      Hahaha, that's my system saying it's 21 hours. I've turned on the speaking clock. I was actually shooting the video at 09:00 PM. Forgot to trim that portion, nice catch. 😅

    • @prakhar992
      @prakhar992 4 года назад +1

      It happened in another video as well😅

  • @charanckck
    @charanckck 3 года назад

    Love you man. Thanks for your time and knowledge.

  • @jaisaram5239
    @jaisaram5239 5 лет назад +1

    let's if we have list item with nested element. eg: item . then how we will figure out and get the id if user clicked on span or etc.

    • @snehashischattopadhyay9519
      @snehashischattopadhyay9519 4 года назад

      You will get it through the event object. As discussed by Akshay, using event delegation attach a single event listener and the event object will contain the element id that is being clicked.

    • @harshithm1739
      @harshithm1739 2 года назад

      @Jaisa Ram, you found any solution for this? I am stuck in the same situation

  • @techbjunks
    @techbjunks 5 лет назад +1

    we can also do event delegation with capturing phase.

  • @iqrarhussain7136
    @iqrarhussain7136 3 месяца назад

    Very beautiful concept, and beautifully taught by Akshay sir.

  • @tiny_mystery
    @tiny_mystery 7 месяцев назад

    Akshay do we need this in react as well or not ?

  • @pbirbal4757
    @pbirbal4757 3 года назад

    Akshay, You literally wear this same t shirt for every video. Is this Uber t shirt or you are into personal branding???

  • @creativemind4116
    @creativemind4116 2 года назад

    Thanks for your valuable explaination.
    now i'm bit confuse between event bubbling and event deligation.

  • @shivGGG
    @shivGGG 4 года назад +1

    Amazing stuff, great way of teaching :)

  • @Rajavinoth1981
    @Rajavinoth1981 5 лет назад +1

    Super

  • @gokulsrinivasan8563
    @gokulsrinivasan8563 2 года назад

    Akshay,Thank you so much these videos. You explanation is pretty clear. Thanks again.

  • @samadhansalgar9181
    @samadhansalgar9181 3 года назад

    !! Its so easy to learn from your @video Thanks lot for such great video !!! ,
    Please cover one fronted template with javscript, Jquery if possible with AJAX, animation
    or only with Javascript with animation

  • @lovishgoel2745
    @lovishgoel2745 3 года назад +1

    best series so far

  • @dhrumit7
    @dhrumit7 2 года назад

    @Akshay Saini
    Your videos are very insightful. Please do one video on Infinite Scroll and Pagination.

  • @ashishchaturvedi3319
    @ashishchaturvedi3319 4 года назад

    Hello brother please make a video on why arrow functions don't bind this??

  • @abhilayman8533
    @abhilayman8533 5 лет назад

    document.getElementById is faster than document.queryselector , so use the first one.

  • @gyanendraverma3155
    @gyanendraverma3155 3 года назад

    best javascript knowledge. I was looking at this kind of depth of knowledge in javascript.

  • @sandeepadiyar001
    @sandeepadiyar001 4 года назад

    Thanks very much akshay. Yours videos are very helpful and quite descriptive and understandable. Can you please make one video on webpage security. I mean from a frontend developer point of view how can I add security to a web app. Thanks in advance

  • @Siddarthathota
    @Siddarthathota 5 лет назад +1

    Awesome explanation man.. kudos

  • @surbhi3369
    @surbhi3369 3 года назад

    what about onChange Event will it work the same way as working for onClick?

  • @starman9000
    @starman9000 4 года назад +1

    Hi why console.log( 0 && 'hi') returns 0 ? can any have any idea?

    • @jojogeorgeyoutube
      @jojogeorgeyoutube 3 года назад +1

      expr1 && expr2
      logical && operator returns expr2 if expr1 evalutes to true; else returns expr1
      0&& 'hi' here 0 evaluates to false so returns 0

    • @starman9000
      @starman9000 3 года назад

      @@jojogeorgeyoutube Thank you!

  • @MrMaulikthaker
    @MrMaulikthaker 3 года назад

    second example when you type anything then that much time function is called. So I think its the case of function re-calling wihile each character we typed in event delegation.

  • @anshulsurana4877
    @anshulsurana4877 2 года назад

    apparently his videos can be watched in 1.5x

  • @suthagarthambikannu6402
    @suthagarthambikannu6402 4 года назад

    nice video and simple understandable English. Very useful for me

  • @hassanforever11
    @hassanforever11 3 года назад +1

    Sir can you make video on why DOM operation are expensive as compared to React virtual DOM. need detaisl on why it is expensive cant find any proper content on why its slow

    • @akshaymarch7
      @akshaymarch7  3 года назад +2

      Noted. Will try to come up with that soon. 😇👍

    • @hassanforever11
      @hassanforever11 3 года назад

      @@akshaymarch7 Thankyou :)

    • @vijaynavale3919
      @vijaynavale3919 3 года назад

      In our regular dom if we make any change on UI it will affect on all other parts of our UI this way our operation speed become slow.
      But in react virtual dom only change/re-render happens when a component has been change and that component only gets rerender not all other components.
      Internally react is fast but still we need take care of rerendering

  • @AdnanAli-cp9hk
    @AdnanAli-cp9hk 4 месяца назад

    amazing amazing amazing!!!!

  • @vishalgoel6323
    @vishalgoel6323 3 года назад

    Hi Akshay, event delegation can't be used when if we need to send some additional information also on clicking every li item.. right ?

  • @md.mohiulislam6516
    @md.mohiulislam6516 2 года назад

    This guy is awesome. Love from Bangladesh.

  • @Priyanka_12321
    @Priyanka_12321 8 месяцев назад

    Thank you so much sir 😀🙏

  • @sumonbarai178
    @sumonbarai178 Год назад

    thanks for totorial.....

  • @videosforinventionimaginat2410
    @videosforinventionimaginat2410 4 года назад

    Sir very nice , video very simple, clear and directly getting into our head
    Sir also make real time video of securing API , and make webpage loading g faster in UI

  • @subhamshaw1726
    @subhamshaw1726 2 года назад

    after namaste javascript, this frontend interview question playlist had helped me a lot. I can apply this things in my work as well as ace my interviews.
    All thanks to YOU, sir

  • @snehashischattopadhyay9519
    @snehashischattopadhyay9519 4 года назад

    bro learning a lot from your videos. Please please please upload more videos on Javascript. Also if possible discuss on tricky CSS, HTML questions.

  • @shubamdadhwal3497
    @shubamdadhwal3497 2 года назад

    Excellent Explanation Akshay, I have a minor doubt - as you have said it we can only do event delegation because of event bubbling, but while playing with the code, when i set the useCapture flag to true, it was working perfectly fine too, can you explain this behaviour?

  • @sunilthind3257
    @sunilthind3257 2 года назад

    @akshay
    Can you please show in a video how can we achieve event delegation in react components

  • @TheRichEng
    @TheRichEng 5 лет назад +1

    Awesome brother

  • @karthikaruna1994
    @karthikaruna1994 5 лет назад

    Does document.querySelector('#form input').addEventListener('keyup', filterElementsAndDoStuff) add separate event listeners for each input? Is this any better than adding separate listeners like document.querySelector('#form #input1').addEventListener('keyup', doStuff), document.querySelector('#form #input2').addEventListener('keyup', doStuff) and so on?

    • @snehashischattopadhyay9519
      @snehashischattopadhyay9519 4 года назад

      It is way better than adding separate event listeners to every form element. Using event bubbling whenever a child element event is triggered it bubbles up to parent which then handles the logic.

  • @shubhsagar6234
    @shubhsagar6234 3 года назад

    Bhai aap ek course banao web development pe plzz

  • @AnasShahidIqbal
    @AnasShahidIqbal 3 года назад

    So in the frameworks like angular where we can loop through the list and attach an event listner on each item does attach a separate event for it or it takes care of delegation internally?

  • @himanshushekhar3694
    @himanshushekhar3694 3 года назад

    such a nice video with great explaination...Thank you so much

  • @pritamchowdhury4191
    @pritamchowdhury4191 4 года назад +1

    It was just fantastic.Thank you so much brother

    • @pritamchowdhury4191
      @pritamchowdhury4191 4 года назад

      can u make a video about how javascript engine works.thanks in advance

  • @AshishPatelIndia
    @AshishPatelIndia 2 года назад

    ❤❤❤

  • @dinesh9896
    @dinesh9896 Год назад

    thank you brother for making it very clear!

  • @profjs2398
    @profjs2398 2 года назад

    excellent video, thank you Akshay

  • @rajashekhar433
    @rajashekhar433 5 лет назад

    if(e.target.dataset && e.target.dataset.uppercase) alert(e.target.dataset.uppercase);

  • @diwakardayal954
    @diwakardayal954 7 месяцев назад

    thanks

  • @TanayBhalani
    @TanayBhalani 3 года назад

    Amazing video, learnt a lot✌️ thank you Akshay 👍

  • @mohann7750
    @mohann7750 4 года назад

    Thanks Dude..Helped me to learn events in javascript in depth..

  • @senaitmekonnen692
    @senaitmekonnen692 3 года назад

    thankyou

  • @12vvs
    @12vvs 4 года назад +1

    Great!

  • @lingrajbiradar4416
    @lingrajbiradar4416 2 года назад

    can u please create a complete playlist of javascript.

  • @sagarkumar4816
    @sagarkumar4816 3 года назад

    awesome video .. very informative .. please make more videos

  • @somhrikkonar6147
    @somhrikkonar6147 2 года назад

    Thanks

  • @simranbhatia2402
    @simranbhatia2402 2 года назад

    Superb

  • @RavindraSingh-zg9eq
    @RavindraSingh-zg9eq Год назад

    nice

  • @vitalvisions4U
    @vitalvisions4U 3 года назад

    Got this too.

  • @bahaakrayem4104
    @bahaakrayem4104 5 лет назад +1

    Great

  • @valurathod8579
    @valurathod8579 4 года назад

    Dude your videos are awesome and useful.. Can you please make video on angular?

  • @vaibhavkhandelwal5771
    @vaibhavkhandelwal5771 3 года назад

    Sir i try it but it doesn't not work give me type error of is not a function

  • @musicsubhankar
    @musicsubhankar 3 года назад

    This was nicely explained. Keep up the good work. :)

  • @DharmendraKumar-qk5bt
    @DharmendraKumar-qk5bt 5 лет назад +1

    It's awesome. Thanks

  • @MujahidAli-gi6jk
    @MujahidAli-gi6jk 2 года назад

    I just only sy kitna awesome parhata ha yar

  • @mohitkushwaha8156
    @mohitkushwaha8156 3 года назад

    Video is very helpful
    grateful to you

  • @codingwithgraceandtruth2456
    @codingwithgraceandtruth2456 2 года назад

    hooked to your tutorials, really good quality in teaching and content.

  • @ishusharma415
    @ishusharma415 3 года назад

    Really useful information and very well explained lectures. One of the best channel to prepare for front end interviews. Thank you so much for creating this channel.

  • @namankeshari7332
    @namankeshari7332 Год назад

    Awesome video once again!

  • @prakhar992
    @prakhar992 4 года назад

    Hi.
    Can you make video on accessing elements in a shadow root?

  • @chamnil8666
    @chamnil8666 4 года назад

    Thank you for making these free videos.They are so educational.keep up the good work.