1

I have a question about the last example on this page: http://api.jquery.com/on/

What is the difference between this:

$( "body" ).on( "click", "p", function() { // p after click

and that?:

$( "body p" ).on( "click", function() { // p after body

After testing myself, i see they behave differently. it seems that the second one does not consider DOM? Thanks!

depperm
  • 10,606
  • 4
  • 43
  • 67
john
  • 21
  • 2
  • 1
    What do you mean "doesn't consider DOM"? – msanford Dec 04 '15 at 19:36
  • The first one is a delegated event handler, which useful for dynamically added elements & second is direct event handler, there is no difference as such in their behaviour, both listens and respond to click event on `p` tags in `body` – vinayakj Dec 04 '15 at 19:39
  • @msanford. meant to say "dynamically added elements" as vinayakj pointed out. after reading the comments in the duplicate that Kevin referenced, things are making sense. just didn't know the right vocabularies such as "direct" and "delegated" to search for the answer. Thx all! – john Dec 04 '15 at 19:52

0 Answers0