The signature for jQuery’s delegate method is documented as such:
.delegate( selector, eventType, handler )
Are there any circumstances that would make the parameter order the following, swapping eventType and selector?
.delegate( eventType, selector, handler )
I have a project that no matter what I do, I have to put the eventType first. For example this is the only thing that works in my current project (this is just a test, not real code!):
$('body').delegate('click', 'input', function (e) {
alert("Why is this the only way it will work for me in this project?");
});
According to the documentation, this should work:
$('body').delegate('input', 'click', function (e) {
alert("This will never get hit");
});
I tested with small projects and it works like the documentation says it should (here’s an example js fiddle: http://jsfiddle.net/wmdYr/16/)
I’m using jQuery 1.7.1 from the Google CDN, and tried a few older versions with the same results.
Is there a reason that the Event needs to be the first parameter in some circumstances?
I resolved the issue and can now use the documented order of parameters for jQuery’s delegate method:
.delegate( selector, eventType, handler(eventObject))The solution for us was to upgrade the jQuery Validate library to the latest version (1.9). The project I am working on used an older version of the jQuery validate library (1.5.5) that shipped with Asp.Net MVC. That version appears to have conflicts with jQuery’s delegate method.
This conflict affects jQuery’s delegate method and Backbone.js view events since Backbone.js uses jQuery’s delegate method under the hood.