Is there any difference between the following code?
$(\'#whatever\').on(\'click\', function() {
/* your code here */
});
and
Is there any difference between the following code?
No, there is no functional difference between the two code samples in your question. .click(fn)
is a "shortcut method" for .on("click", fn)
. From the documentation for .on():
There are shorthand methods for some events such as .click() that can be used to attach or trigger event handlers. For a complete list of shorthand methods, see the events category.
Note that .on()
differs from .click()
in that it has the ability to create delegated event handlers by passing a selector
parameter, whereas .click()
does not. When .on()
is called without a selector
parameter, it behaves exactly the same as .click()
. If you want event delegation, use .on()
.