Let's say I have some code like this:
$('#mywrapper')
.on('click', 'button', myFunction)
.on('change', 'input', myFunction)
;
Is there a more efficient way to wire both event and selectors up to the same function?
Let's say I have some code like this:
$('#mywrapper')
.on('click', 'button', myFunction)
.on('change', 'input', myFunction)
;
Is there a more efficient way to wire both event and selectors up to the same function?
Nope, not really actually. The only real variation here is that you can pass an object to .on() that will define all your events in one go:
$("div.test").on({
click: myFunc,
mouseenter: myFunc
});
You can use a 'plain object' to pass both at once:
$('#mywrapper').on({
click: myFunc,
mouseenter: myFunc
});
jQuery .on() (example towards the bottom)