How can an event bubble to document but not to document.body?

  • A+

Recently I ran into some interesting behaviour related to event-bubbling. I have created a code-pen to illustrate this:

All I am doing there is bind two event-listeners (or I guess three including the one that removes the button). One on the document and one on the document.body.

When clicking on the button, only the console.log from the document would show up.

Why? Wouldn't the event bubble to document.body first and then to document?

Or asked differently: how can an event bubble up to document but not stop by document.body?

$(document).on("click", "button", () => console.log("document knows"));  $(document.body).on("click", "button", () =>   console.log("document body knows") );  $("button").on("click", e => $("button").remove());
<script src=""></script> <button>Click me</button>


TL/DR: The event bubbles to both body and document. The button is not in DOM at that moment. But for body, before handler execution jQuery tries to ensure that button exists inside it. For document, id does't do such a check.


I'm not sure why it's designed like this. It's just explanation why it happens.

First of all, the handlers are bound to document and body respectively, not to the button. When event bubbles to the document, jQuery tries to find descendants, specified by selector (button in our case) and executes the handler against each of them. Same for body. The difference is in the way how it checks the descendants.

When the event is bubbled to document and body, the button is already removed from DOM, but the button element still exists in memory and is accessible via

for both document and body jQuery searches the elements to execute the handler against:

  1. adds (our removed button)
  2. adds all buttons found inside parent (body or document) (founds none)
  3. filters the result (which is just our button) by some matchers to check if the buttons are really children of parent
  4. Executes the handlers against found elements.

The difference in 3rd step.

2 pieces of jQuery code are responsible for this:

outermostContext = context === document || context || outermost;

var ret = ( !leadingRelative && ( xml || context !== outermostContext ) ) || (                 (checkContext = context).nodeType ?                     matchContext( elem, context, xml ) :                     matchAnyContext( elem, context, xml ) ); 

First piece sets outermostContext variable.

For document, the outermostContext is true, for body it's body.

Second piece sets variable ret which decides will the handler be called or not.

For body, context !== outermostContext is false so matcher proceeds to 'matchContext' and eventually returns false.

For document, context !== outermostContext is true so the matcher returns true without context check.

The meaning of this is that for body it tries to ensure that button exists inside it. For document - it does not.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: