Are the docs for jQuery’s on() function incorrect (or unclear)? Consider this code:
<div>
<span>
<div>
<input type="button" value="click me!" />
</div>
</span>
</div>
$(document).on("click", function() {
console.log(this.toString());
});
The docs state
selector A selector string to filter the descendants of the selected
elements that trigger the event. If the selector is null or omitted,
the event is always triggered when it reaches the selected element.
Clicking the button only causes one console.log for the document itself, while $(document).on("click", "*", function()... causes many.
I know the Stack Overflow community isn’t responsible for the jQuery docs, but shouldn’t they say that when the selector is omitted, the event is only triggered when it reaches the selected element? Or is there something about event delegation I’m not understanding correctly?
There is no event delegation when you leave the selector out. From the docs:
Basically
.onis doing the jQuery thing — it is overloaded to do completely different things depending on arguments. Personally I prefer.delegatefor delegation, and.bindfor normal events, as they are much clearer and I hope they don’t get removed in later versions.