I’m loading part of my webpage using AJAX, in particular jQuery.load(). With this the usual jQuery pattern
$('.classname').click(...) // Handler
// or, working with bootstrap
$("a[rel=tooltip]").tooltip() // Function
or similar obviously don’t work any more, because they are called only when the page is loaded. I realize there is jQuery.on for the first example, but how would I implement the second?
Is there a simple (builtin) way to also apply these to jQuery.loaded stuff, or do I have to work around it myself? Seems like a problem a lot of people should be having.
You have to work around it yourself; but you can easily to this by calling
$("a[rel=tooltip]").tooltip()in the callback forload():Be sure to restrict the
tooltip()call to only newly loaded elements, or you’ll end up initializing tooltip multiple times per element (which may result in weird behaviour).To avoid duplicating code you just need to define a helper function;
Which allows you to init
tooltip. The parameter is optional, and lets you restrict initialization to only descendants of the provided element, e.g.: