JavaScript DOM Tutorial #10 - Event Bubbling

แชร์
ฝัง
  • เผยแพร่เมื่อ 30 ม.ค. 2025

ความคิดเห็น • 62

  • @bartoszsowa5140
    @bartoszsowa5140 2 ปีที่แล้ว +2

    I've been watching your tutorials for about 1,5 year now but still i'm regularly getting impressed by how clearly you explain concepts

    • @NetNinja
      @NetNinja  2 ปีที่แล้ว +2

      Thanks Bartosz!

  • @NoahNobody
    @NoahNobody 7 ปีที่แล้ว +36

    I never understood what people meant by event bubbling until I heard that explanation.

  • @LucasofAppalachia
    @LucasofAppalachia 6 ปีที่แล้ว +5

    Your tutorials are absolutely amazing mate! Very clear and easy to understand. JavaScript has been my Achilles Heel for the last year, but thanks to your tutorials it's becoming much clearer.

  • @patrickc.6183
    @patrickc.6183 5 ปีที่แล้ว +6

    I love this! Such a smart way of adding event listeners!

  • @MdR1328
    @MdR1328 5 ปีที่แล้ว +3

    Just learned about event bubbling but couldn't really wrap my head around it yet ;D
    Thank you for this clear explanation!

  • @dunkTheFunk
    @dunkTheFunk 3 ปีที่แล้ว

    Thanks, we live in great times. Your tutorials are priceless.

  • @albertbaarsma
    @albertbaarsma 5 ปีที่แล้ว +6

    I'm really liking this series so far. So clear! You make it seem so simple :) should have done this earlier!

    • @NetNinja
      @NetNinja  5 ปีที่แล้ว +1

      Thanks Albert, glad you’re liking it 😊

  • @superyoyo486
    @superyoyo486 5 ปีที่แล้ว

    I was struggling to understand parentNodes, childNodes,etc...consequently, event bubbling..but you made it so simple to understand, thanks alooooooot

  • @عجائبالدنيا-ي6ح
    @عجائبالدنيا-ي6ح 4 ปีที่แล้ว

    every time i watch one of your tutorials i add many experiences to my knowledge, please keep to add more value videos

  • @study_with_thor
    @study_with_thor 3 ปีที่แล้ว +1

    ur born to teach this !

  • @rongliao9255
    @rongliao9255 5 ปีที่แล้ว +1

    Wow! Very good tutorial in explaining often confusing or intimidating topic! Thanks a lot!

    • @NetNinja
      @NetNinja  5 ปีที่แล้ว

      Thanks Rong 😊

  • @DeViLTh0rn
    @DeViLTh0rn 4 ปีที่แล้ว

    Thank you. A newbie guy from 🇲🇾...

  • @Meyaleleth
    @Meyaleleth 4 ปีที่แล้ว

    Thank you!!! The only explanation I fully understood.

  • @jfiejka
    @jfiejka 7 ปีที่แล้ว +3

    Hey man,
    I believe in that case you can just simply use remove() method without grabing parent node. Correct me if I'm wrong :)
    P.S. I really love your channel, keep up good work sir!

    • @MouradBougarne
      @MouradBougarne 7 ปีที่แล้ว +2

      You are right, if he use : e.target.parentElement.remove();
      But the purpose of the course is to show how to traversing through the DOM.

    • @aqhumza
      @aqhumza 7 ปีที่แล้ว

      Wrong, it should be like this: e.target.parentElement.parentElement.remove();

    • @btm1
      @btm1 5 ปีที่แล้ว +2

      it's because remove() is not supported in older/some browsers

  • @axelcarvalho2661
    @axelcarvalho2661 6 ปีที่แล้ว

    Great. I learned about it but till now didn't know why it's used.

  • @pastuh
    @pastuh 6 ปีที่แล้ว +1

    Very well explained. Actualy i had problem and was thinking.. why it not works..

  • @juangrandoso6950
    @juangrandoso6950 7 ปีที่แล้ว +5

    WebAssembly? Please, it would be awesome if someone like you explains it

  • @shanebenlolo4866
    @shanebenlolo4866 3 ปีที่แล้ว

    made perfect sense thanks so much!

  • @edgarhernandez-kl2ci
    @edgarhernandez-kl2ci 3 ปีที่แล้ว

    Good explanation

  • @AzwadAbid
    @AzwadAbid 6 ปีที่แล้ว

    Very helpful tutorial. Thanks bro.

  • @mwala94
    @mwala94 7 ปีที่แล้ว

    Great video as always! :)

  • @boboboboboboo7522
    @boboboboboboo7522 6 ปีที่แล้ว

    very good explanation

  • @yadusolparterre
    @yadusolparterre 5 ปีที่แล้ว

    "What is event bubbling? In basic terms it's the bubbling of events"
    Wow, so clear now.

  • @Gruximillian
    @Gruximillian 7 ปีที่แล้ว

    Hi Shaun, another great playlist! I would suggest to keep li.parentNode.removeChild(li) since that way the event handling function can be reused on some other elements without the need to change variable 'list' inside the function, or at least to better decouple it from the variables outside its scope, a situation that I often encountered in my experience.

  • @kshitijbaria732
    @kshitijbaria732 2 ปีที่แล้ว

    thanks sensei

  • @kalamashaka8280
    @kalamashaka8280 6 ปีที่แล้ว +2

    I HAVE INCLUDED BREAK DOWN AND EXPLANATION OF THIS EXAMPLE : HOPE IT HELPS SOMEONE
    'use strict'
    //STEP : 1
    //so our task is to fire an click event on all buttons so that when the button is clicked the whole li is deleted
    //first thing below is to get all the li elements
    //below i have gotten and put all my buttons under deleteBtn variable
    let deleteBtn = document.querySelectorAll('#book-list .delete'); //buttons gotten
    //STEP : 2
    //From the group of buttons elements we got, we can now apply or fire the event to a single button element or button
    // so below we convert the nodelist or html collection into an array and sork on a single button
    Array.from(deleteBtn).forEach(function(button){
    //STEP : 3
    //for every single button we click let us apply an event to that particularly clicked button
    button.addEventListener('click', function(e){
    //get the event to work on the item that was selected in our case li
    //once the button is clicked then we want all the entire li element to be the target of the event
    //STEP : 4
    const li = e.target.parentElement;
    //STEP : 5
    //below we call the action that we want the event to perform on the single element
    //now call the parent of the button and remove the particular childelement selected in our case the entire li
    li.parentNode.removeChild(li);
    });
    });

  • @saulblancotejero8298
    @saulblancotejero8298 4 ปีที่แล้ว

    You can use 'this' inside the event to use the list as well, right?

  • @anthonyhaddad5630
    @anthonyhaddad5630 5 ปีที่แล้ว +1

    u rock mate!

  • @QuanDev2
    @QuanDev2 3 ปีที่แล้ว

    what's the theme you were using in Atom?

  • @kimdh377
    @kimdh377 5 ปีที่แล้ว

    where could I find the skill at 2:46 when you set your mouse on the line, then it becomes purple color? I really need that skill TT

    • @simple8810
      @simple8810 3 ปีที่แล้ว

      simple css on hover it border color will be colored

  • @MegaLearningStuff
    @MegaLearningStuff 6 ปีที่แล้ว +1

    This worked for me,
    const list = document.querySelector('#book-list ul')
    list.addEventListener('click',function(e)
    {
    if(e.target.matches('.delete'))
    {
    const li = e.target.parentElement;
    li.parentNode.removeChild(li);
    }
    });

    • @MegaLearningStuff
      @MegaLearningStuff 6 ปีที่แล้ว +1

      the code in here in fourth line if(e.target.className == 'delete') { } was not working for me.

    • @faithfuledema8645
      @faithfuledema8645 2 ปีที่แล้ว

      Very helpful

  • @mohammadshayegh7763
    @mohammadshayegh7763 6 ปีที่แล้ว

    OMG!!!! AMAZING

  • @mahsam7975
    @mahsam7975 7 ปีที่แล้ว

    hi, i dont get the 2:57 !
    why if we add another li manually the first code snippet dont run for it? i watched many times and i still dont get it
    he says because we lready associated event to those .delete buttons but also the new list item can act like others !
    help please

    • @BookOfSaints
      @BookOfSaints 6 ปีที่แล้ว +6

      This is because, initially, you are cycling through all the *existing* li elements in the book list and adding an event listener to each one. When you add a new li element, it does not have an event listener added to it.
      The way the second approach works is adding a click listener to the entire ul element, containing all the li elements. When you click anywhere in the in the ul, the event will fire, then check to see if the click event happened on one of the delete buttons. If so, it will remove the li that corresponds to that delete button.

    • @samaneh77
      @samaneh77 6 ปีที่แล้ว +1

      yes, even if we add a new button the way he did, because that button also has the .delete class it should be selected in the forEach loop and get an eventListener; I also did not get why he said it will not have the event listnere...

    • @flamingobird8513
      @flamingobird8513 6 ปีที่แล้ว +1

      Same here! Can anyone explain it? Thank you

    • @Toulkun
      @Toulkun 2 ปีที่แล้ว +1

      @@BookOfSaints So forEach works only for already existing elements. Would it work with for loop if we set condition to be less than li's length?

    • @partoftheabsoluteone4960
      @partoftheabsoluteone4960 ปีที่แล้ว

      I agree on this point. I am still not clear on this, even after reading the explanation below about 'existing' li elements. Why does the forEach() loop not see or pick up the new li element when we refresh and run the code again? It still has a class name of 'delete'

  • @karthikalluri8188
    @karthikalluri8188 ปีที่แล้ว

    do u have the code for that webpage

  • @MrRSharma07
    @MrRSharma07 6 ปีที่แล้ว

    Can anyone explain me how that ' e ' Argument of function is working , i mean from where it's getting value ?
    I am beginner , sorry for stupid question

    • @chethankambi31
      @chethankambi31 5 ปีที่แล้ว

      addEventListener(), takes 2 arguments - one is the event(activity) that took place which in this case is the 'click' ('click' is defined as one of the events, for a complete list refer mozilla documentation) and the second argument is a callback function that can do something on the event that just took place. Hope this helps!

  • @davideugene7911
    @davideugene7911 7 ปีที่แล้ว

    Merci :)

  • @nikkoalminar7924
    @nikkoalminar7924 6 ปีที่แล้ว

    Is .target a method?

  • @guitar602991
    @guitar602991 7 ปีที่แล้ว

    Hello, would this be a much more expensive way to delete the item?
    var listItems = document.getElementsByTagName("li");
    for (var i = 0; i < listItems.length; i++) {
    listItems[i].onclick = function() {this.parentNode.removeChild(this);}
    }

    • @chethankambi31
      @chethankambi31 5 ปีที่แล้ว

      Yes, because you will be looping every time an event occurs!

  • @hakunamata_tuh
    @hakunamata_tuh 7 ปีที่แล้ว

    bubbling :)

  • @CastleShield
    @CastleShield 5 ปีที่แล้ว

    ❄️

  • @The5starSTUNNNA
    @The5starSTUNNNA 7 ปีที่แล้ว +2

    Please MEAN stack

    • @pikachu5223
      @pikachu5223 7 ปีที่แล้ว +5

      MERN BETTER FOR ME :)

  • @k4qdex
    @k4qdex ปีที่แล้ว

    this hardly explains bubbling...