Is it considered bad practice to use jQuery's .on()
event handler for every event?
Previously, my code contained script like this:
$('#cartButton').click(function(){
openCart();
});
However I've recently started using InstantClick (a pjax jQuery plugin).
Now none of my scripts work. I understand why this is happening, but I cannot wrap my code with the InstantClick.on('change', function(){
tag as this means my code starts to repeat itself. For example, clicking on the cart button will run the openCart()
function many times. So to get around this, I'm changing all my functions to something like this:
$(document).on('click', '#cartButton', function(){
openCart();
});
I'm curious as to whether this will increase loading times and cause excess strain. Is it bad practice to use the on()
event handler for every event in my code?
click()
function uses theon()
function under the hood. – Airspace