Event Capturing, Event Bubbling and jQuery.on()
Asked Answered
S

2

14

I have an interesting question about event capturing, bubbling and jQuery.on().

I have recently learned more about the difference between event capturing and event bubbling and how the two flow differently in and out of the child-parent elements within the DOM.

So to add an event listener with "event capture direction" i would use:

element.addEventListener("click", myFunction, true);

and to add an event listener with "event bubble direction" i would use:

element.addEventListener("click", myFunction, false);

This is all good and well, but what I want to know is when using jquery.on() to add event listeners, how does one specifiy the event direction in terms of capturing and bubbling?

currently I am using something like:

$('parent selector').on('click', 'child selector', function(){alert('just alert something already...');});

How do I tell jQuery that it needs to add these event listeners in the "event capture direction" or the "event bubble direction"?

Sinistrodextral answered 10/2, 2014 at 15:13 Comment(0)
D
20

You can't. jQuery events works with event bubbling it doesn't support capturing.

Also see

Dictate answered 10/2, 2014 at 15:17 Comment(2)
Hi Arun. I wanted to nominate you for community moderator but you are missing convention badge. Are you close to achieving it? (10 posts with score >2 on meta).Crossman
@ArunPJohny I get it now. :)Calton
D
0

You can use e.target to get the exact element that was clicked

  $('.parent').on('click', function(e){
    if(e.target.classList.contains('child'))
      // do something
    }
  })
Dominoes answered 30/8, 2015 at 14:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.