JQuery - Passing event.data to .on () and .bind ()

As I understand it, there will be no jQuery in future versions bind

, so I am trying to use on

. However, I ran into a problem.

I'm trying to figure out why bind

it lets you install event.data

, but on

no.

It works

$(document).bind('ajaxError', '#form-id', function(event, jqxhr, settings, exception){
  $(event.data).render_form_errors($.parseJSON(jqxhr.responseText));
});

      

This does not work

$(document).on('ajaxError', '#form-id', function(event, jqxhr, settings, exception){
  $(event.data).render_form_errors($.parseJSON(jqxhr.responseText));  
});

      

I have multiple forms on the same page, so I am trying to display the error for each specific form.

+3


source to share


1 answer


This is because the second argument to on () is a selector and not event data (as it on()

also means 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));  
});

      

Note that, to my knowledge, no official source has said that future versions of jQuery won't have bind()

. on()

is indeed intended to replace both bind()

and delegate()

, but neither method has been deprecated, and both are likely to remain for the foreseeable future (there is a lot of code that still uses them).

+4


source







All Articles