2

Is there any difference between :

$('selector').change(function() {});

and

$('selector').on("change", function() {});

And if there is, which one should I use in most cases?

4
  • 4
    No difference, and use whichever you prefer Commented Jul 13, 2016 at 8:25
  • I think no difference Commented Jul 13, 2016 at 8:25
  • i think click work on already exits elements and onclick also work with dynamically generated elements
    – webdevanuj
    Commented Jul 13, 2016 at 8:27
  • @AnujKhandelwal You need to pass selector parameter to on() method to delegate it. This is not the case here
    – A. Wolff
    Commented Jul 13, 2016 at 8:29

3 Answers 3

2

No difference, internally change function would use .on(...) to bind the respective event.

function (data, fn) {
    return arguments.length > 0 ? this.on(name, null, data, fn) : this.trigger(name);
}

Source

1

The is no difference at all, they both trigger the same javascript function. You can use both in any browser that supports the addEventListener modules.

Check the list of browsers that support it: http://caniuse.com/#feat=addeventlistener

However to remove the event, you'll have to use .off( "change" ) as the jquery doc suggests

Hope it helps

1

in accordance to method documentation $('selector').change( function() {}) is just a shortcut for $('selector').on("change", function() {});. indeed this is the same.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.