Reputation: 1924
As I understand it, future versions on jQuery will not have bind
so I am trying to use on
instead. I have run into a problem though.
I am trying to understand why bind
allows me to set event.data
but on
doesn't.
This works
$(document).bind('ajaxError', '#form-id', function(event, jqxhr, settings, exception){
$(event.data).render_form_errors($.parseJSON(jqxhr.responseText));
});
This doesn't work
$(document).on('ajaxError', '#form-id', function(event, jqxhr, settings, exception){
$(event.data).render_form_errors($.parseJSON(jqxhr.responseText));
});
I have multiple forms on a single page so I am trying to render the error for each specific form.
Upvotes: 3
Views: 135
Reputation: 263047
That's because the second argument to on() is a selector, not the event data (as on()
is also meant to supersede delegate()).
The correct syntax for on()
in your case is:
$(document).on('ajaxError', null, '#form-id', function(event, jqxhr, settings, exception) {
$(event.data).render_form_errors($.parseJSON(jqxhr.responseText));
});
As an aside, note that, to my knowledge, no official source has said that future versions of jQuery will not have bind()
. on()
is indeed meant to supersede both bind()
and delegate()
, but neither method is deprecated and both will most probably remain around in the foreseeable future (there is a lot of code out there that still uses them).
Upvotes: 4