Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript $(document).on() vs $('#id').on() Reply To: $(document).on() vs $('#id').on()

#167867
dyr
Participant

In the first case you’re attaching the event listener to the document, which listens for an event originating from an element with a particular id that bubbles up the DOM.

In the second case, you’re attaching the event listener to the element itself.

Go here: https://api.jquery.com/on/ and read the section with the heading “Direct and delegated events.”

Cheers :)