3

I have the following HTML code:

<ul id="level1">
     <li id="level21">item1</li>
     <li id="level22">item2</li>
</ul>

I tried to do event bubbling for the child class <li> using jQuery:

$(document).ready(function(){
    $('#level1').on('click', function(event){
         alert($(this) + ',' + event);
    });
});

This code does not trigger event bubbling for both <li> class. Is this because the child classes do not have click events for them? I still don't understand when event bubbling would be triggered?

Abhishek Ghosh
  • 2,593
  • 3
  • 27
  • 60
blank_kuma
  • 335
  • 6
  • 16
  • Bubbling occurs from innermost child to parent. So if you define it on inner `li`; click will trigger of inner `li` first and then on subsequent parents. – Shaunak D Apr 11 '15 at 14:59
  • thanks for the fast post. i am aware that clicking on the `
  • ` will trigger the event as well but why do I not see the a chain of events that start from the li and go to the parent class ie. two alerts, one for the child `
  • ` and one for the parent `
      `?
  • – blank_kuma Apr 11 '15 at 15:08
  • 1
    Have a look at http://www.quirksmode.org/js/events_order.html and http://stackoverflow.com/questions/4616694/what-is-event-bubbling-and-capturing – Andreas Apr 11 '15 at 15:12
  • So basically event bubbling always happens when an event is triggered. In my code, if I were to assign another click event on the `
  • ` elements, that would trigger two alerts right? but I still don't understand why a click event is triggered for the `
  • ` element even though I didn't assign it one? there is no event capturing in jQuery.
  • – blank_kuma Apr 11 '15 at 15:42