When using jQuery on(), why use (document) vs. the element itself?

Both of those are valid.

The former works for dynamically added elements. You use document because you’re delegating events on children of the document object, so events bubble up to the document level. It’s also more convenient to select the closest parent you can (and the parent must exist on the page at load).

The latter still works, and is a preferred way to simply bind events to specific elements.

I personally don’t recommend delegating through the document object, but rather the closest parent that exists on page load.

Here are the docs for on().

Leave a Comment