I have a question about jQuery load best practice. Imagine the following situation:
$("#div").on('click', function() {
$('#result').load('test.html');
});
The code works fine, the load method executes, but now the loaded content can’t be accessed by a jQuery plugin. I solved like this:
$("#div").on('click', function() {
$('#result').load('test.html', function(){
$('.element').plugin();
});
});
My question is simple, is there a better way to solve this situation ?
Ideally you would need to target the
parentof the element withid="div"… let’s say a container<div id="wrap">for this example. Then use.on()using theparentcontainer as selector like:then the loaded content can be accessed by your other plugin.
This format of using
.on()actually replaced.live()since jQuery v1.7.x.Eventually, you could also do