I previously used:
delegate("input:last", "change", handler);
It worked for me in 1.4.4, but does not in 1.7.2 (if the widget does not contain the last input on the page).
I see a couple tickets requesting the selector to apply to the delegated element's context (#10762, #6696), although they are closed due to the invalid child selector usage. I don't want to wade into that battle, so I'm talking specifically about the selector context, not the use of the CSS child selector.
I'll be honest, I remember when I first wrote this code a year or two ago I wondered if the context would be valid. I tested it skeptically.
But I've used it quite a bit since then, and I think it makes sense for several reasons:
- $("table tr:even").click is a pretty common sort of sort of thing you'd want to be able to delegate in a templated/AJAX document.
- Using the document context diminishes the performance of the is() evaluation in question, for potentially significantly for frequent events in large documents. This could be a significant detractor for another scenario for replacing traditional multi-element.
- The current functionality is less deterministic than the previous.
- Not using the delegated element's context makes a number of useful jQuery selectors non-deterministic within page widgets, devaluing useful jQuery code.
- It worked differently in the original implementation of delegate.
- It seems to follow the spirit of jQuery context better, both from the standpoint of the event handler the delegate is replacing, and the broader usage of context in general (where specificity is not 'lost' when possible).
- Requiring the parent selectors to be repeated both on the delegated element's selector and in the delegate function call makes for less DRY code.
- The discussion in prior tickets seemed to agree that the selector context was sensible, even if the use of the CSS child selector wasn't.
I don't understand why v. 1.7.2 line 3285 says:
jqcur.context = this.ownerDocument || this;
since I don't understand it, I didn't pretend to 'fix' it. Sorry to not contribute more than this defect report.