on
is bound only for existing elements, if you want to make it listen to elements that were inserted after you useon
you have to add a selector argument.
With a selector argument it's a delegate event, while without it's a direct event.- If you used
on
delegate event signature the callback is bound to the selector which theon
was called upon$('thisSelector')
, while it will listen to the events of the elements in the selector argument like
$('foo').on('event', 'thisSelector', func)
. - For better performance you should attach the
on
function to the closest static element to the newly inserted elements, this is actually the main reason whylive
was removed from jQuery in version 1.9,live
attaches the listener to thedocument
which has enormous performance penalty.
What we learned from the above, you must make sure .pagination
is already in the DOM when you call on
, and it shouldn't be replaced.
Now you should come with something like:
$("{staticElement-closeToTheNewElements}").on("click", '.pagination a',callback);