What is the best way to execute jQuery.change ()

I have two separate scripts that essentially do the same thing. I built them over time and just discovered that I am using a couple of different tools to achieve the same result.

I want to standardize and use best practice in both cases.

One way to check for a change event:

$('input[name="status"]').change(function() {}); 

Another way that I test the change event is as follows:

 $("#email").bind("change", function(e) {}); 

Which way is better? What is the difference between 2?

Thank you for helping me to understand it.

+8
jquery onchange
Apr 03 2018-12-12T00:
source share
1 answer

Before jQuery 1.7, change() was just a short cut for bind("change") .

Starting with 1.7, however, on() was introduced and bind() preferred. This means that change() is a shortcut to on("change") , and in fact all calls to bind() will now call on() inside.

In short, they do the same thing. I believe that explicitly using on() (or bind() ) is preferable, but as long as you are consistent with your entire code base, I see no real differences.

It can be argued that using change() over on("change") "better" because a typo in the word "change" will cause a parsing error in the first instance ("undefined is not a function") but will fail with on() ... but obviously your unit tests will catch this, right?;.)

+16
Apr 03 2018-12-12T00:
source share



All Articles